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 thesuperclass of the subclass in which it is used. This second form of super is most applicableto 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 , fromsuperclasses to subclasses. Further, since super() must be the first statement executed ina 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 ofObject. That is, Object is a superclass of all other classes. This means that a referencevariable of type Object can refer to an object of any other class. Also, since arrays areimplemented as classes, a variable of type Object can also refer to any array.
Комментарии
Отправить комментарий