super
It can be used in two ways:
- Calling Superclass Constructors
The statement super() or super(arguments) must appearin the first line of the subclass constructor and is the only way to invoke a superclass constructor.
A constructor may invoke an overloaded constructor or its superclass's constructor. If neither of them is invoked explicitly, the compiler puts super() as the first statement in the constructor.
In any case, constructing an instance of a class invokes the constructors of all the superclasses along the inheritance chain.A superclass's constructor is called before the subclass's constructor. This is called constructor chaining.
- Calling Superclass Methods
To invoke a overridden method defined in superclass, from inside a overriding method in subclass, you must use super.
an a subclass of Circle access the toString method defined in the GeometricObject class using a syntax such as super.super.toString()? No. This is a syntax error.
Can an instance of Circle invoke the toString method defined in the GeometricObject class? No, not any more, since toString() in GeometricObject has been overridden in Circle.
Overriding vs. Overloading
- An instance method can be overridden only if it is accessible. Thus a private method cannot be overridden, because it is not accessible outside its own class. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.
- Like an instance method, a static method can be inherited. However, a static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. The hidden static methods can be invoked using the syntax SuperClassName.staticMethodName.
Data and Methods Visibility
Polymorphism
subtype and supertype
A class defines a type. A type defined by a subclass is called a subtype, and a type defined by its superclass is called a supertype.
Polymorphism means that a variable of a supertype can refer to a subtype object.
A method can be defined in a superclass and overridden in its subclass.
declared type and actual type
The type that declares a variable is called the variable's declared type.
The instance may be created using the constructor of the declared type or its subtype. The actual type of the variable is the actual class for the object referenced by the variable.
Which method is invoked by an object is determined by its actual type. This is known as dynamic binding.
Matching a method signature and binding a method implementation are two separate issues. The declared type of the reference variable decides which method to match at compile time. The compiler finds a matching method according to the parameter type, number of parameters, and order of the parameters at compile time. A method may be implemented in several classes along the inheritance chain. The JVM dynamically binds the implementation of the method at runtime, decided by the actual type of the variable.
Abstract Class
- An abstract method cannot be contained in a nonabstract class. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be declared abstract. In other words, in a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass. Also note that abstract methods are non-static.
- An abstract class cannot be instantiated using the new operator, but you can still define its constructors, which are invoked in the constructors of its subclasses. For instance, the constructors of GeometricObject are invoked in the Circle class and the Rectangle class.
- A class that contains abstract methods must be abstract. However, it is possible to declare an abstract class that contains no abstract methods. In this case, you cannot create instances of the class using the new operator. This class is used as a base class for defining a new subclass.
- A subclass can override a method from its superclass to define it as abstract. This is very unusual, but is useful when the implementation of the method in the superclass becomes invalid in the subclass. In this case, the subclass must be defined as abstract.
- A subclass can be abstract even if its superclass is concrete. For example, the Object class is concrete, but its subclasses, such as GeometricObject, may be abstract.
- You cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type. Therefore, the following statement, which creates an array whose elements are of GeometricObject type, is correct.
You can then create an instance of GeometricObject and assign its reference to the array like this:GeometricObject[] objects = new GeometricObject[10];
objects[0] = new Circle();