aspose file tools*
The moose likes Beginning Java and the fly likes Construction/Variables Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Construction/Variables Question" Watch "Construction/Variables Question" New topic
Author

Construction/Variables Question

Daniel Hirning
Ranch Hand

Joined: Sep 16, 2013
Posts: 50
    
    1

Hi All,

Trying to get my head around Java constructors. Say i have a couple classes linked through polymorphism.

Animal,Feline,Cat.





Animal - has its instance variables set.
Feline - the no arg constructor passes values to the String/Int constructor?
Cat - has a default only constructor and receives the values from Feline through polymorphism.

If however i pass the following values into the constructor call.


How does cat inherit the values 100,John?
Are the values passed to Feline not inheritable, or is it something to do with the default Cat constructor and the no-arg Feline constructor?

Any help would be appreciated,

Daniel

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5018
    
    8

A few things going on here that you need to note:

1. Constructors are not inherited
2. A constructor will call super() by default if you don't call another constructor with parameters
3. When you redeclare instance members (name, age), they shadow/hide the parent fields that have the same name. There's a difference between shadowing and hiding so I'll let you research which one it is because I'm honestly not sure right now which is the case here.


So, when you declare Cat the way you did, Java provides a default no-argument constructor Cat(). That default constructor calls super(), which is Feline(). Feline() calls its super(), which is Animal() its other constructor with "John" and 100. Feline(name, age) calls Animal() but then sets its own copies of name and age with "John" and 100, respectively. With shadowing/hiding going on, a new Cat instance ultimately inherits the values "John" and 100 from its parent, Feline. When you create a Feline with "Jeff" and 200, this is a different instance and is totally isolated from any other Feline or Cat instance that you might create. Remember that a class is only a "cookie cutter" for instances. It's a template for what all instances of the class will look like. Each instance of a class gets its own set of values for instance members though. They are not shared between instances.

The way you have written these classes, a Cat instance can only ever have the name "John" and an age of 100 right after it has been instantiated. You can, however, change the values directly since you did not declare your fields as private.

That is, you can have this code:



BTW, the OO mechanism at play here is not polymorphism, it's inheritance.


Junilu - [How to Ask Questions] [How to Answer Questions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
That code is designed to go with your classes; compile and execute it, and see what happens. You can see what Junilu means about hiding.
Daniel Hirning
Ranch Hand

Joined: Sep 16, 2013
Posts: 50
    
    1

Campbell/Junilu,

Thanks, inheritance was my concern not Polymorphism..late nights what can i say

Also I am aware the lack of logic behind the constructors.
Eg. Which ones had values, which ones didn't, how the feline survived to 100/200 etc.
I was just trying to create scenarios to work out what i didn't understand.(what would make my understanding fail) Turned out I didn't have to do much..

A rewording perhaps (if I'm lucky) but it helps me think..

So when i called Feline fe = new Feline().
It called Feline() which called Feline(name,age) which called Animal() which called Object().

When I passed it construction values in the call.
It called Feline(name,age) which called Animal() which called Object().

So the instance of Cat() did not get a copy of those in Feline(name,age) but from Feline().

Does that sound right?

Shadowing, in this example would be the parameter name in the constructor being the same as the instance variable. The one in scope at the time shadows the other correct? Though this is resolved with this. name = name; does it not?

Campbell, your demo code appears to cast the Feline variable name, to its superclass Animal, which produces the instance name declared in Animal. Is this what you meant by hiding? Could you elaborate on what is going on here, I assume the instance of Feline is not overwritten or anything, and that 'tiddles is just hidden behind the couch for now'?

thanks in advance,
Daniel
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5018
    
    8

"Tiddles" is not hiding at all. There is only ever one instance of the object. When you instantiate a Feline, there is just a Feline and nothing else. The Feline just happens to have inherited behaviors and attributes of Object and Animal. The cast simply says that rather than treating Tiddles as the Feline that it is, treat it like it's just some Animal instead. This brings out the Animal attributes that Tiddles the Feline inherited. This has to do with static vs dynamic binding, something that I'll again leave for you to research
Daniel Hirning
Ranch Hand

Joined: Sep 16, 2013
Posts: 50
    
    1

So 'hidden' in this case refers to what happens when a sub class has the same variable name as a super class. The super class version gets hidden.
While 'shadowing' in this case refers to having the same variable name in a method/constructor as an instance variable. The one in scope shadows out the other.

so..Campbell was showing me that the Animal name variable was not overridden, just hidden when he used the Feline reference but used the inherited Animal variables?

Junilu,

I love these little research assignments, you actually learn a lot this way, one of the best things about this site!

Static v Dynamic Binding.

So Campbells example..


Would be a case of static binding, as it is based on an instance variable and can be resolved at compile time. As a static it uses the declared type instead of the reference type. In this case Animal.

If there was a non static method called getName() that i used instead of a variable, it would be Dynamic and based on the reference type.

Any flaws with that or have i got mixed up?

Thanks for helping BTW its much appreciated.

Daniel

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Daniel Hirning wrote: . . .

so..Campbell was showing me that the Animal name variable was not overridden, just hidden when he used the Feline reference but used the inherited Animal variables?

. . .
Yes.

I would have to check the nomenclature, but I think you are correct with everything there. It also shows how you can get confused if you use the same variable name twice.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Daniel Hirning wrote: . . .
If there was a non static method called getName() that i used instead of a variable, it would be Dynamic and based on the reference type.
. . .
[My underlining.]
No, that bit is incorrect. Try this:-The result of that method depends on the runtime type, not the reference type. The runtime type here is Feline and the reference type is Animal. You get the Feline behaviour.
Daniel Hirning
Ranch Hand

Joined: Sep 16, 2013
Posts: 50
    
    1

Sorry, I meant the object type new Feline() I just stuffed my 'nomenclature', you would be a nightmare at scrabble I think!

Thanks for the help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Daniel Hirning wrote: . . . you would be a nightmare at scrabble I think!
More like Scrabble would be a nightmare. I always lost.
Daniel Hirning wrote:Thanks for the help.
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Construction/Variables Question