Hi folks,
Please post some answers to the following questions, related to today's lecture. You can either try to deduce the answers, or find out more online, or both. If you find a webpage you think is useful, you may post the URL as well as (but not in place of) your answer. Suppose class C extends class B extends class A, like in lecture.
We talked a bit today about how "any C is a B" and "any B is an A", which allows you to do assign objects of a subclass into a variable whose type is a superclass. Why does this force subclasses to have all the methods of the superclass? Why doesn't it force subclasses to have all the constructors of the
superclass?
On a related note, if I type this:
B myB = new C("Mr. C");
myB.printName( );
Will this execute the printName( ) method defined in B.java or C.java?
Why is this a related note?
What if I now pass myB as the argument to this method:
public int ageOf( A someA ) {
someA.printName( );
return someA.age;
}
Which printName( ) method gets called now?
Can the variable age, defined in A.java ever be "overridden" in any sense in B.java or C.java? If so, explain how as clearly as you can. If so, is there a way to access the values from the superclasses?
Please post some answers to the following questions, related to today's lecture. You can either try to deduce the answers, or find out more online, or both. If you find a webpage you think is useful, you may post the URL as well as (but not in place of) your answer. Suppose class C extends class B extends class A, like in lecture.
We talked a bit today about how "any C is a B" and "any B is an A", which allows you to do assign objects of a subclass into a variable whose type is a superclass. Why does this force subclasses to have all the methods of the superclass? Why doesn't it force subclasses to have all the constructors of the
superclass?
On a related note, if I type this:
B myB = new C("Mr. C");
myB.printName( );
Will this execute the printName( ) method defined in B.java or C.java?
Why is this a related note?
What if I now pass myB as the argument to this method:
public int ageOf( A someA ) {
someA.printName( );
return someA.age;
}
Which printName( ) method gets called now?
Can the variable age, defined in A.java ever be "overridden" in any sense in B.java or C.java? If so, explain how as clearly as you can. If so, is there a way to access the values from the superclasses?
for the first question
ReplyDeleteif A is the super and B and C are the subclasses that extend A the reason for the subclasses to have all the methods of the super is because of inheritance
a good webpage I found on inheritance
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
what this means is that the super or "parent" class will extend its method to the subclasses this allows modularity and keeps code from becoming redundant by creating the same method over and over
I am using chapter 3, and specifically section 3.3.3, of the assigned text to answer these questions.
ReplyDelete>"On a related note, if I type this:
>B myB = new C("Mr. C");
>myB.printName( );
>Will this execute the printName( ) method defined in B.java or C.java?"
This will execute the printName( ) method of C.java. Java will use the "actual class of the object" (direct quote) to determine which method to use when invoked from an object reference.
>"What if I now pass myB as the argument to this method:
>public int ageOf( A someA ) {
> someA.printName( );
> return someA.age;
>}
>Which printName( ) method gets called now?"
I am less sure on this one, but I think that C.java's implementation will still be called, as the object is still actually of type C. However, the member variable age that is returned will be that of A.java, as Java returns variables based on the "declared type of the reference" (direct quote) used.
I'm not sure what the answer to the question, "Why is this a related note." Also, is this question referencing Java's object's polymorphism, or the statement about superclasses forcing subclasses to inherit all the methods but not constructors?
Zach, your answer is fine, as far as it goes.
DeleteOne thing: the original post never said that age is a member variable. How will the inheritance situation be different if it is instead a class variable?
The "related note" question is referring to the issue of subclasses needing to inherit all methods, but not constructors.
The methods/members/fields "inherited" (through polymorphism, with emphasis on the poly prefix) from a superclass depends on the supertype and subtype. It follows that, a subclass of an interface must implement all its methods (and use the subclass' constructor since interfaces don't have constructors). Alternatively, a subclass of a "regular" class can override selected methods of the super (think feb15 lab). When in doubt, read documentation on the polymorphic properties specific to one's implementation.
ReplyDeleteIt's been established that inheritance changes based on object type. Constructors are not inherited, period. This makes perfect sense. A constructor defines behavior(s) specific to that object type, and subclasses can call the constructor of a super from inside its constructor, which enables more functionality than hypothetically overriding a constructor (Java 101: instantiation of an object calls its constructor).
When a supertype is instantiated as a subtype, the constructor of the supertype is called and the supertype's behavior is that of the subtype. The behavior of the subtype it that of the supertype and the overridden behavior(s) of the subtype.
So myB.printName() calls printName() defined inside the C(). Even if printName() is not overridden, myB is defined to behave like a C(), which already inherits the members/methods of itself.
Same thing for the second example, but B() inherits from A() (printName() called from B() ). By using protected getters/setters, the age variable of the supertype can be accessed/changed by subclasses in a "safe" way.
Rob, you make some good points, but it is also rather hard to tell what you mean by some of your sentences. Inheritance and polymorphism are basically separate concepts in Java (unlike in C++). You get polymorphism by implementing Interfaces.
DeleteI don't know what you mean by "inheritance changes based on object type."
To say that a subclass CAN call the constructor of its parent class is true, but not quite right. Actually, a subclass ALWAYS calls ONE OF the constructors of its parent class, as the first step in each of its constructors. If you leave out this line invoking "super", then the Java compiler puts in a call to super( ) for you (the default, no-argument constructor of the parent class).
Rob, in paragraph 5, you say something about the safety issues in allowing the subclass to access member variables. This is a good point, but I really just wanted to raise the issue of how inheritance of fields differs from inheritance of methods.
"Can the variable age, defined in A.java ever be "overridden" in any sense in B.java or C.java? If so, explain how as clearly as you can. If so, is there a way to access the values from the superclasses?"
ReplyDeleteYou cannot override member fields in Java. You can, however, hide them by overriding the field's respective getter/setter. If you refer to the method of an object that is contained in that object's superclass, you'll end up getting results that reflect the superclass's member fields, so you have to override the appropriate method in order to use a subclass's fields.
Inheritance in java by definition passes all the member fields and member methods from the superclass to the subclass. The constructor is not a member method so the subclass does not inherit it, but it can be invoked.
ReplyDeletereference: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
Dean Dominguez:
DeleteAlso, as we learned in our last lab, we may not want to inherit Constructors even if we could. Some subclasses may require more information to be passed that the parent didnt allow for (ie boolean crotchRocket). Also, we had to get cute with Truck/SemiTruck constructor set up because of the different number of tires issue.