In a hierarchy of classes, it is possible for both super class and sub class to have their own constructors - one in the super class and the other one in the sub class. Now, when we create an object of the super class, the super class constructor is responsible for creating the super class object.
Suppose, we create the object of the sub class, both constructors are responsible for creating the object of the sub class. The constructor of the super class constructs the super class portion of the object, and the constructor of the sub class constructs the sub class part of the object.
super() method & super keyword
When both the super class and the sub class define their own constructors, it is the responsibility of the sub class to invoke the super class constructor before it does its initialization of the sub class portion of the object. This process of calling the constructor of the super class from the constructor of the sub class is done with the help of super() method.
Calling the method super() without arguments will call the default constructor of the super class, whereas calling super() with arguments will invoke a parameterized constructor of the super class for initializing the super class for initializing the super class portion of the object.
'super' keyword acts as a reference to the super class object. Using the keyword - super, we can access the members of the super class (both variables and methods). super keyword is also required to access the members (variables/methods) of the super class when they are hidden by the members of the sub class by name.
Order of Execution of Constructors
At the time of object creation of a sub class, the order of execution of constructors (of base class and derived class) is determined based on their order of derivation. They are executed in the same order of their derivation, that means, base class constructor will be executed first, followed by the constructor of the derived class.
Since super() must be the first statement to be called in the sub class' constructor, the order is the same whether or not super() is called explicitly from the sub class. If super() is not used as the first statement, the default constructor of the super class will be called implicitly.
Super class Reference and Sub Class Object
In general, an object reference variable can refer only to object of its type. However, a reference variable of a super class can be used to point to the object of any of its sub classes derived from it. In other words, a super class reference can refer to the object of any of its sub classes.
It is important to note that it is the type of the reference variable, and not the type of object that it referes to - determines the members that can be accessed in an object created in a hierarchy of objects. That means, when a reference to a super class is assigned a reference to the object of its sub class, it can have access only to those members of the object defined by the super class.
Method Overriding
In a class hierarchy, when a method in a sub class has the same name, return type and parameters as same as that of its super class, then the method of the sub class is said to override the method of the super class.
When the overridden method is called from an object of sub class, it will always refer to the overridden method of the sub class. The base class version of the overridden method in the sub class is hidden after overriding.
To prevent a method from being overridden in a sub class, use the keyword - final as a modifer at the start of its declaration in the base class. Methods declared as final can't be overrdden in a sub class.
Abstract class
Abstrat class is a super class that has one or more abstract methods declared in it. An abstract class determines the generalized form of one or more behavior of a class of objects. It doesn't provide the implementation of one or more of its methods, leaving it to each sub class to fill in the details.
An abstract class can't be used for object creation. Instead it can be used to define another class that implements the code for all abstract methods of the base class, and then the objects be created using the derived class. That means, objects can't be directly created using an abstract class, but through a child class that implements all the abstract methods of the base class.
The Object Class
Java has a special class class Object class, which is at the top of the hierarchy of classes defined in Java. By default, Object class is the super class of all other classes in the hierarchy, i.e., all other classes are sub classes of Object class. And hence, a reference variable of type Object can refer to an object of any other class in Java.
Some of the methods defined in Object class are as follows:
- Object clone() - This method creates a new object that is same as the object being cloned
- boolean equals(Object object) - determines whether one object is equal to another
- void finalize() - This is the last method to be called before an unused object is recycled. This method can be used for freeing the resources utilized by the object.
- class <?> getClass() - obtains the class of an object at run time
- int hasCode() - returns the has code associated with the invoking object
- String toString() - returns a string that describes the object