Page 449 - Computer science 868 Class 12
P. 449
12.1.1 Advantages of Inheritance
Inheritance is a strong feature of object oriented programming which offers the following advantages.
• Reusability of code: We can add additional features into an existing class without modifying it by deriving a new class
from an existing one. The new derived class will have the combined features of both the classes. This also makes
coding simpler as we need not reprogram or redesign our existing class. Rather, the modifications are made in a new
class which is then connected with the previous class by this mechanism. It thus saves programmers time and effort.
• Extendability: We can extend the existing classes by adding some new features.
• Maintainability: The program code is small and divided into a separate class, thus makes the debugging and maintaining
the program easy.
• Transitive nature: If a class derived from a base class becomes the base class of another derived class, then the new
derived class can access the properties of the previous base class too. Like a father inherits the properties of his
father. When son acquires his father’s properties, then he has access to his grandfather’s properties also.
• Automatic reflection: Any changes made in the base class are automatically reflected to all its derived classes.
• Data hiding: Base class can keep some data private so that it cannot be altered by the derived class.
12.1.2 Keyword Extends
The extends keyword in Java indicates that the derived class inherits the properties of the base class. It helps to
establish a hierarchical relationship between two classes.
The syntax of establishing relationship between two classes using the keyword extends is
class derived class extends base class
The derived class which has inherited the properties of the base class is thus an extended version of its parent class. It
can also define its own properties as per the requirement. However, the base class can only use its own properties and
does not have access to the extra variables or methods of the derived class.
The following points should be kept in mind while using the extends keyword.
• Java does not support multiple inheritance. So, a derived class cannot extend properties of multiple base classes.
• Multiple derived classes can inherit the properties of a single base class.
• If a base class is declared as final, then it cannot be inherited by using the extends keyword.
• The extends keyword can be used to inherit the interfaces in Java/[Interface will be discussed in detail in the later
part of this chapter.]
12.1.3 Keyword Super and Method Overriding
The super keyword in Java is a reference variable which is used to refer to the variables and methods of the base class.
It is mainly used to prevent naming conflict. The super keyword is used in the following cases:
• When we have variables having same name in both derived and base classes, then the super keyword is used to
access the base class variables. If super keyword is not specified, then the base class properties cannot be accessed
as Java gives priority to local variables.
• When we have methods having same name and signature, one belonging to the base class and other to the derived
class, then the method of the derived class is said to override the methods of the base class and is executed. This
concept is called method overriding. Generally, the base class variables and methods are hidden. The derived
class object executes its own methods and the base class methods are not invoked by the Java compiler. However,
the use of super keyword allows the derived class object to execute the base class methods. To call a super class
methods we write super.method().
447
Inheritance, Interfaces and Polymorphism 447

