This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP: playing with inheritance and when what calls what. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP: playing with inheritance and when what calls what." Watch "SCJP: playing with inheritance and when what calls what." New topic
Author

SCJP: playing with inheritance and when what calls what.

Alan Dee
Greenhorn

Joined: Aug 11, 2011
Posts: 7
I'm studying for the SCJP exam and have been experimenting with inheritance. I came up with this class and a set of examples. I'm wondering of someone would be willing to confirm my explanation and/or expand on why the outputs are what they are.
I know some are obvious but the intent is to switch them up in flash cards.

Thanks

bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
Since the array is an Animal type, instance variables will use Animal
however methods will run the method of the object

There is only method overriding in java and no variable overriding. So when you are accessing methods the method of the actual object is called, and for instance variables it depends on the reference type.

When accessing static variables or methods it will be implicitly cast as the type of the variable


I am not sure if you can say that as a cast. The compiler will replace with the reference type.
Scotty Mitchell
Ranch Hand

Joined: Aug 09, 2011
Posts: 46
Since the array is an Animal type, instance variables will use Animal
however methods will run the method of the object.


(I'm also studying to take the OCPJP exam in a month or so...just started reading K&B 4 days ago....on chapter 6 now)

From my experiance programming, which is only a couple years, "overriding" variables in subclasses like that is bad practice, but of course no compiler errors will result from it. Technically, I would say that "overriding" is not what is happening in the real Java sense this is more related to "hiding".

My understanding is that inheritance deals with methods, and class specific "behaviors". This explains why you would want to have methods run based on the object type not a reference as you may want specialized behaviors in your subclasses. Trying to override instance variables in subclasses would mean you are attempting to have inheritance through states, which doesnt really make sense to me...I'd think youd want each of your classes to represent their states mutually exclusively, and have only common behaviors represented nonexclusively.

It is true that since the array type is of type Animal that the instance variable calls will use Animal, unless casted explicitly to be of a specific Horse or Persian. Of course calling a subclass method of Animal is showing how Java implements method overriding, and changing behavior to be type specific as desired for inhertiance purposes.

When accessing static variables or methods it will be implicitly cast as the type of the variable.


So anything related to the keyword static would indicate it is related to the class, and not an instance of the class as far as I know. Hence, calling a static method NEEDS to be replaced otherwise it doesn't even make sense when it refers to a specific instance of a class. I'm not sure if this is the correct way to think about it, but I imagine class variables as being totally unrelated to the instance of a class. The compilers we use are nice enough to do this replacing for us I would tend to agree that cast may not be the correct word to use. A cast would mean that its specifying type based on a supertype or vice versa depending on cast direction. What is happening is here isn't really that I don't think. Correct me if I'm wrong...

Alan Dee
Greenhorn

Joined: Aug 11, 2011
Posts: 7
Thanks for your responses. So maybe a better way to put it is:

Properties are not overridden in Java. Since the array is of type Animal all properties accessed in the array will be of an Animal object.
However, methods can and are overridden. So when a Persian is stored in an Animal array it is the Persian method that is executed.
When accessing static methods and properties the compiler will replace the referencing object with the class type, in this case Animal.

Sound Good?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SCJP: playing with inheritance and when what calls what.
 
Similar Threads
Polymorphism
method overriding
Polymorphism Teaser
SCJP for Java 6 book by Sierra and Bates covariant return code example question