mudda yard
mudda yard

Reputation: 11

super() constructor within a subclass that extends a subclass?

I'm not exactly sure if I worded my question right, but I'm confused with these lines of code.

public class First {
    public String name() {
        return "First";
    }
}

public class Second extends First {
    public void whoRules() {
        System.out.print(super.name() + " rules");
        System.out.println(" but " + name() + " is even better");
    }
    public String name() {
        return "Second";
    }
}

public class Third extends Second {
    public String name() {
        return "Third";
    }
}

Second varSecond = new Second();
Third varThird = new Third();
varSecond.whoRules();
varThird.whoRules();

When the above is executed, it prints out

First rules but second is even better

First rules but third is even better

why wouldn't it be:

First rules but second is even better

Second rules but third is even better

Can a subclass be a superclass for another class? Or can there only be one(superclass)? (Using example code above) I understand that First is a superclass for Second, but is Second a superclass for Third? Or is First the superclass for Third?

If there were 10 classes that extended off each other (second extends first, third extends second, etc) Would the superclass for all of those classes be First?)

Upvotes: 1

Views: 2008

Answers (4)

Shurmajee
Shurmajee

Reputation: 1047

This is simple Inheritance. First is a super class of Second and Second is a super class of Third. Basically when you create an object of class Third it will inherit all the (non-private) properties from parent classes that is both class First and Second.

First -> Second -> Third 

The Super keyword - In the given example you are overriding (implementing a super class method in the subclas) the method name() of super class First in the subclass second.Using the keyword super.name() lets you call the name() method of the super class.

And the most important thing I would want you to remember is

The java compiler calls a method based on the Reference type and not the object type

when you say

varSecond.whoRules();

In this case the object reference belongs to class Second

System.out.print(super.name() + " rules");//calls the name() in class First
System.out.println("but" + name()+"is even better");//calls the name() in class Second

but when you say

varThird.whoRules();

In this case the object reference belongs to class Third

System.out.print(super.name() + " rules");//calls the name() in class First
System.out.println("but" + name()+"is even better");//calls the name() in class Third

Simply because the name() method is defined in class Third. If you had not defined the name() method in class Third it would have called the super class method .

and answering your other question there is no hard limit on the depth of inheritance but normally you don't see 10 super classes in a hierarchy in a good design.

Upvotes: -1

aymankoo
aymankoo

Reputation: 671

it's because your calling super from the Second class, which calls super.name() from First class. the super of second is always first

Upvotes: 0

Deepak
Deepak

Reputation: 327

Third varThird = new Third();

Since Third class does not have the whoRules() method. It calls the superclass method.

If no object is specified during the call, the compiler uses "this" keyword to call. So thats why, this.name() printed "Third" as whoRules() method is called by third Class object.. Hence the result - "First rules but third is even better."

Upvotes: 1

Andy Thomas
Andy Thomas

Reputation: 86391

Why wouldn't it be: ... "Second rules but third is even better"

Because super.name() in class Second refers to the superclass of Second, not the superclass of the instance.

From the Java Language Specification: The form super.Identifier refers to the field named Identifier of the current object, but with the current object viewed as an instance of the superclass of the current class.

Can a subclass be a superclass for another class?

Yes.

Or can there only be one(superclass)?

Each class can have at most one direct superclass, which in turn may have its superclass, etc.

(Using example code above) I understand that First is a superclass for Second, but is Second a superclass for Third? Or is First the superclass for Third?

Both First and Second are superclasses for Third.

Second is the direct superclass for Third.

Upvotes: 3

Related Questions