Change Contents of the Bubble
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Exam 2 Review Fall2007:Understanding inheritance

Post your questions here.
Back to Exam Review 2

leo.introduce();
I’m Leonardo

batman.introduce();
Hello. I am Batman. And I am weak.


Not sure about this guys.

you use "super" to invoke the superclass method's behavior. You must put it in the constructor and it has to be the first line of the constructor. so like

public introduce(someName){
super(someName)}

this should call introduce in this class like it calls introduce in the superclass.


As for "which method will get called if the method is defined in both parent and child classes?"

it's whichever one you set the variable equal to. If you
//Class student extends Person and both have a greet() method
Student thenry = new Student("thenry");
thenry.greet();

then it will call the greet as defined in Student, not Person. However if you do

Person thenry = new Student("thenry");
thenry.greet();

it still calls greet() from Student and not Person, so my question is how can you set a variable to Person yet make it equal Student?


1) you can invoke the superclass method's behavior. by putting Super(); in the method of the Subclass

2) Say you have 2 classes,

a Car class, which is abstract

a Honda class, which is the subclass of the Car class

now, say the Car Class methods are abstract such as MAKE and MODEL, that means that every subclass of that class should define the methods Make and Model.

why do you need an Abstract Car Class? because you don't want to instantiate the Car class it self, you want to get more specific. Meantime you want every subclass of the Car class such as Honda, to define their Make and Model. That is why you introduce abstract methods such as make and model so that every subclass would be required to redefine those methods.(Every Subclass of a Abstract Superclass has to redefine all the abstract methods of the parent class)

Or in other words, Abstract makes an outline for all the subclasses

[I know I'm getting close to reality, but I don't know if I'm there yet!]

so if there is a student and person class and both have greet, is the following correct?
Student REMOVED = new Student; REMOVED.greet() will greet w/ students' method
Person REMOVED = new Student; REMOVED.greet() will greet w/ students' method
Person REMOVED = new Person; REMOVED.greet() will greet w/ persons' method

yes



Link to this Page