Inheritance

     You can only specify one superclass for any subclass that you create. Java does not support the inheritance of multiple superclasses into a single subclass.

    Super. Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword

super.  super has two general forms. The first calls the superclass' constructor. The second is used to access a member of the superclass that has been hidden by a member of a subclass.

package main;

public class Car {

private int a;

public Car(int a) {
this.a = a;
}
}
package main;

public class BMW extends Car{

public BMW(int a) {
super(a);
}
}
Also, super() must always be the first statement executed inside a subclass constructor. 

The second form of super acts somewhat like this, except that is always refers to the 
superclass of the subclass in which it is used. This second form of super is most applicable
to situations in which member names of a class hide members by the same name in the superclass.
package main;

public class Car {
int a;
}
package main;

public class BMW extends Car {
int a;

BMW(int a, int b) {
super.a = a;
a = b;
}
}

When Constructors Are Executed.
    In class hierarchy, constructors complete their execution in order of derivation , from
superclasses to subclasses. Further, since super() must be the first statement executed in
a subclass' constructor, this order is the same whether or not super() is used. If super() 
is not used, then the default or parametrless constructor of each superclass will be executed.
package main;

public class A {
A() {
System.out.println("Inside A's constructor.");
}
}
package main;

public class B extends A {
B() {
System.out.println("Inside B's constructor.");
}
}
package main;

public class C extends B {
C() {
System.out.println("Inside C's constructor.");
}
}
The output will be:
Inside A's constructor.
Inside B's constructor.
Inside C's constructor.

There is one special class, Object, defined by Java. All other classes are subclasses of 
Object. That is, Object is a superclass of all other classes. This means that a reference 
variable of type Object can refer to an object of any other class. Also, since arrays are 
implemented as classes, a variable of type Object can also refer to any array. 

Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class