Inheritance is one of the most dominant and vital feature of the object oriented programming because it supports the hierarchical classifications, reusability of class; and defined to specialization. Java is a language that supports inheritance but with some additional advantages and features.
Here we create a generalized class that have some set of related items. A class that is derived from any another class (may be generalized class) is called a subclass or derived class or extended class or child class. The class from which a subclass is derived is called superclass or base class or parent class. A subclass is a specialized version of the it's parent class, and it inherits all the members
Each class has a superclass, and each class can have one or more subclasses. There is a hierarchical relation between the classes. Java supports single and multilevel inheritance.
The class Object is at the top of the Java class hierarchy. It is the most general class, which defines the specific behavior required by all object. If you are not extending any class then that class will automatically extends the Object class by the compiler. Excepting Object class, every class has one and only one direct superclass (single inheritance). Only Object class does not have any superclass. In the absence of any other explicit superclass, every class is implicitly a subclass of Object.
Whenever you create a new class, you define all the related information like fields, methods, and nested class. But in case of inheritance, when we create a new class and if there is an existing class having some of the code that you want, you can derive your new class from the existing class with some additional code (includes instance fields and methods).
The class is a collection or a wrapper of the methods and fields. The question arise here that how these members (fields and methods) are accessible from outside the class. The description of how these members are predictable to behave is called class contract. To define the class's contract is the responsibility of the class designer. Extending of class provides two types of inheritance as given below:
- 1.Inheritance of type or contract: The subclass obtains the type of the superclass.
- 2.Inheritance of implementation: The subclass obtains the implementation of accessible fields and methods of superclass.
The class can be extended for a number of purposes. The extended class (subclass) is called the specialized version of the superclass; this subclass defines new behavior. Yet you can change the implementation of some inherited method, but keep in mind, we can change it within in a limit. The changes in implementation must not violate that contract. The access control mechanism work when interacting or accessing the members. Each class provides two different contracts - one is for the users of the class and other is for the extenders of the class.