Author Topic: I am confused  (Read 4843 times)

0 Members and 1 Guest are viewing this topic.

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
I am confused
« on: April 06, 2006, 01:54:29 pm »
Part of Child Program:
Quote
//********************************************************************
//  Dog.java       Author: Lewis/Loftus/Cocking
//
//  Represents a dog, which is a pet.
//********************************************************************

public class Dog extends Pet
{
   private int weight;

   //-----------------------------------------------------------------
   // Creates a dog with the given name and weight.
   //-----------------------------------------------------------------
   public Dog(String dogName, int dogWeight)
   {
      super(dogName);
      weight = dogWeight;
   }

All of parent:
Quote
//********************************************************************
//  Pet.java       Author: Lewis/Loftus/Cocking
//
//  Represents a pet.
//********************************************************************

public abstract class Pet
{
   private String name;

   //-----------------------------------------------------------------
   //  Creates a pet with the given name.
   //-----------------------------------------------------------------
   public Pet(String petName)
   {
      name = petName;
   }

   //-----------------------------------------------------------------
   //  Returns this pet's name.
   //-----------------------------------------------------------------
   public String getName()
   {
      return name;
   }

   //-----------------------------------------------------------------
   // Returns a string representation of this pet.
   //-----------------------------------------------------------------
   public String toString()
   {
      return "pet " + name;
   }

   //-----------------------------------------------------------------
   // This method should return a string indicating what this pet says.
   //-----------------------------------------------------------------
   abstract public String speak();

   //-----------------------------------------------------------------
   // This method should return a string indicating how this pet moves.
   //-----------------------------------------------------------------
   abstract public String move();
}
Anyways, I am completly confused as to how when we pass the dogs name into the child program that somehow the dogs name makes it over to pet and then is understood to be put into petName without having dogName = petName.

How does it know to go into Pet() and put into petName and not look for dogName?
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: I am confused
« Reply #1 on: April 06, 2006, 02:28:53 pm »
This expresses Java's feature of object-oriented languages we call inheritance.  It expresses the "IS_A" relationship.

First of all, we see:
Quote
public class Dog extends Pet
which means that whatever a Pet does, a Dog does -- and more!

There are several ways to describe this; Dog is a child class or subclass of Pet, while Pet is the parent or superclass of Dog (in C#, we also call Pet the base class, which is a keyword in C#).  In either case, Dog IS_A Pet.

I want to make sure you know what a constructor is.  A constructor is a special type of method used when you're initializing a new object instance.  Your Dog class has a constructor; it's the one with no return type that you emphasized.

When you're inside a constructor, you can call a specific constructor for the parent class, so that the parent class's data gets initialized as well.  In this case, calling super(dogName) tells the compiler to bind the call to the Pet(String) constructor (we know this because the superclass of Dog is Pet, and the dogName variable is typed String). 

To get the name of the pet, you call the Pet class's getName() function.  Because the Pet class defines this function, and its data, you don't need to do so in the Dog class.  This is another feature of object-oriented programming, called encapsulation.  Encapsulation allows us to treat objects like a black box; we don't need to know anything about how the data is stored internally, just how to access it.  In this case, the Pet class encapsulates the "name" property of the Pet object and all of its descendant classes.

You can implement your own Name property in the Dog class also, by adding an additional variable and the getName() function to the Dog class; IIRC, all methods in Java are virtual by default.  But that's inefficient - the Pet class already defines a way to get to that data.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: I am confused
« Reply #2 on: April 06, 2006, 09:03:12 pm »
Hrmm..

So what if I had a new String in the dog class that wanted to find out the dogs color? 
So the child class would look like:
Quote
//********************************************************************
//  Dog.java       Author: Lewis/Loftus/Cocking
//
//  Represents a dog, which is a pet.
//********************************************************************

public class Dog extends Pet
{
   private int weight;

   //-----------------------------------------------------------------
   // Creates a dog with the given name and weight.
   //-----------------------------------------------------------------
   public Dog(String dogName, int dogWeight, String dogColor)
   {
      super(dogName);
      super(dogColor);
      weight = dogWeight;
   }
Then would I have to make another method inside the parent class that looked specifically for the color name, since both the name and the color are strings?  And if so, how would I do that to the parent class?

I just don't understand how the parent class would know how to put both of the different information into the right variable in the right method without a mixup since they are both strings, when the parent and child class don't have similar variables.
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: I am confused
« Reply #3 on: April 06, 2006, 09:13:51 pm »
Hrmm..

So what if I had a new String in the dog class that wanted to find out the dogs color? 
So the child class would look like:
Quote
//********************************************************************
//  Dog.java       Author: Lewis/Loftus/Cocking
//
//  Represents a dog, which is a pet.
//********************************************************************

public class Dog extends Pet
{
   private int weight;

   //-----------------------------------------------------------------
   // Creates a dog with the given name and weight.
   //-----------------------------------------------------------------
   public Dog(String dogName, int dogWeight, String dogColor)
   {
      super(dogName);
      super(dogColor);
      weight = dogWeight;
   }
Then would I have to make another method inside the parent class that looked specifically for the color name, since both the name and the color are strings?  And if so, how would I do that to the parent class?

I just don't understand how the parent class would know how to put both of the different information into the right variable in the right method without a mixup since they are both strings, when the parent and child class don't have similar variables.

No, the class would look like this:
Code: [Select]
public class Dog
    extends Pet
{
    private int weight;
    private String color;

    public Dog(String dogName, int dogWeight, String dogColor)
    {
        super(dogName);
        this.weight = dogWeight;
        this.color = dogColor;
    }

    public int getWeight() { return weight; }
    public String getColor() { return color; }
}

If you wanted the Pet class to manage the Color property instead of the Dog class (so that all pets had a color specified), you'd modify the Pet class to be the following:
Code: [Select]
public abstract class Pet
{
   private String name, color;

   public Pet(String petName, String petColor)
   {
      name = petName;
      color = petColor;
   }

   public String getName()
   {
      return name;
   }
   
   public String getColor()
   {
      return color;
   }

   public String toString()
   {
      return "pet " + name;
   }

   abstract public String speak();

   abstract public String move();
}
...and you'd modify the Dog class to read:
Code: [Select]
public class Dog
   extends Pet
{
   private int weight;

   public Dog(String dogName, int dogWeight, String dogColor)
   {
      super(dogName, dogColor);
      weight = dogWeight;
   }
   // implement abstract methods, too!
}

The Java keyword super is just an alias for the parent class's constructor.  Remember a class can have multiple constructors defined, and the super() parameter list must match one of the parent class's constructors.  Omitting a super() call is bad programming practice because it may leave your object's state partially undefined.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: I am confused
« Reply #4 on: April 06, 2006, 09:18:14 pm »
*Except when you're just deriving from Object or a set of interfaces.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: I am confused
« Reply #5 on: April 06, 2006, 09:21:01 pm »
Hrmm..

So what if I had a new String in the dog class that wanted to find out the dogs color? 
So the child class would look like:
Quote
//********************************************************************
//  Dog.java       Author: Lewis/Loftus/Cocking
//
//  Represents a dog, which is a pet.
//********************************************************************

public class Dog extends Pet
{
   private int weight;

   //-----------------------------------------------------------------
   // Creates a dog with the given name and weight.
   //-----------------------------------------------------------------
   public Dog(String dogName, int dogWeight, String dogColor)
   {
      super(dogName);
      super(dogColor);
      weight = dogWeight;
   }
Then would I have to make another method inside the parent class that looked specifically for the color name, since both the name and the color are strings?  And if so, how would I do that to the parent class?

I just don't understand how the parent class would know how to put both of the different information into the right variable in the right method without a mixup since they are both strings, when the parent and child class don't have similar variables.

No, the class would look like this:
Code: [Select]
public class Dog
    extends Pet
{
    private int weight;
    private String color;

    public Dog(String dogName, int dogWeight, String dogColor)
    {
        super(dogName);
        this.weight = dogWeight;
        this.color = dogColor;
    }

    public int getWeight() { return weight; }
    public String getColor() { return color; }
}

If you wanted the Pet class to manage the Color property instead of the Dog class (so that all pets had a color specified), you'd modify the Pet class to be the following:
Code: [Select]
public abstract class Pet
{
   private String name, color;

   public Pet(String petName, String petColor)
   {
      name = petName;
      color = petColor;
   }

   public String getName()
   {
      return name;
   }
   
   public String getColor()
   {
      return color;
   }

   public String toString()
   {
      return "pet " + name;
   }

   abstract public String speak();

   abstract public String move();
}
...and you'd modify the Dog class to read:
Code: [Select]
public class Dog
   extends Pet
{
   private int weight;

   public Dog(String dogName, int dogWeight, String dogColor)
   {
      super(dogName, dogColor);
      weight = dogWeight;
   }
   // implement abstract methods, too!
}

The Java keyword super is just an alias for the parent class's constructor.  Remember a class can have multiple constructors defined, and the super() parameter list must match one of the parent class's constructors.  Omitting a super() call is bad programming practice because it may leave your object's state partially undefined.
OOOOOOOOOOOOOh  that makes things a lot clearer.  Thanks MyndFyre!

*Except when you're just deriving from Object or a set of interfaces.
And I don't really know about that yet, but I don't have to worry about it now. :D

The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: I am confused
« Reply #6 on: April 06, 2006, 09:49:54 pm »
*Except when you're just deriving from Object or a set of interfaces.
And I don't really know about that yet, but I don't have to worry about it now. :D
Much like how all roads lead to Rome, all classes ultimately derive from java.lang.Object.  You don't need to call super() when your class is just a user class (user classes derive from Object unless they explicitly derive from another class).

Interfaces are just methods; they don't define any data and they don't define constructors, so you don't need to call super() when you're implementing an interface either.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.