2. Inheritance
Defn.- Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object.
It is an important part of OOPs (Object Oriented programming system).
Important terminology:
● Class
● Super Class:
● Sub Class
3. CLASS
● Defn. - A class is a group of objects which have common properties. It is a template or blueprint from which
objects are created.
4. Super-Class/Parent Class
● Super Class: The class whose features are inherited is known as super class(or a base class or a parent class).
5. Child Class/Subclass
● Sub Class: The class that inherits the other class is known as subclass(or a derived class, extended class, or
child class). The subclass can add its own fields and methods in addition to the superclass fields and methods
6.
7. Types of Inheritance
♦ Single Inheritance
♦Multiple Inheritance
♦Multilevel Inheritance
♦Hierarchical Inheritance
♦Hybrid Inheritance
8. Single Inheritance
Defn. - When a class extends another one class only then we call it a single inheritance.
♦Class B extends only one class which is A. Here A is a parent class of B and B would be a child class of A.
10. Multiple Inheritance(Through Interfaces)
Defn.- In Multiple inheritance ,one class can have more than one superclass and inherit features from all parent classes.
♦Java does not support multiple inheritance with classes. In java, we can achieve multiple inheritance only through
Interfaces.
12. Multilevel Inheritance
Defn. - One can inherit from a derived class, thereby making this derived class the base class for the new class.
A is Superclass to B => B is child of A.
B is Superclass to C => C is child of B and Grandchild of A,
14. Hierarchical Inheritance
Defn. - One class serves as a superclass (base class) for more than one subclass.
♦The class A serves as a base class for the derived class B,C and D.
16. Hybrid Inheritance (Through Interfaces)
Defn. - It is a mix of two or more of the above types of inheritance.
A => B,C (Hierarchical Inheritance)
B,C => D (Multiple Inheritance)
18. Important Points
● Default superclass: Except Object class, which has no superclass, every class has one and only one direct
superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a
subclass of Object class.
● Superclass can only be one: A superclass can have any number of subclasses. But a subclass can have only
one superclass. This is because Java does not support multiple inheritance with classes. Although with
interfaces, multiple inheritance is supported by java.
● Inheriting Constructors: A subclass inherits all the members (fields, methods, and nested classes) from its
superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the
superclass can be invoked from the subclass.
● Private member inheritance: A subclass does not inherit the private members of its parent class. However, if
the superclass has public or protected methods(like getters and setters) for accessing its private fields, these
can also be used by the subclass.
19. What can be done in a Subclass
● The inherited fields can be used directly, just like any other fields.
● We can declare new fields in the subclass that are not in the superclass.
● The inherited methods can be used directly as they are.
● We can write a new instance method in the subclass that has the same signature as the one in the superclass,
thus overriding it (as in example above, toString() method is overridden).
● We can write a new static method in the subclass that has the same signature as the one in the superclass,
thus hiding it.
● We can declare new methods in the subclass that are not in the superclass.
● We can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using
the keyword super.
20. POLYMORPHISM (In Java)
Defn. - Polymorphism in Java is a concept by which we can perform a single action in different ways.
1. ♦ In Java polymorphism is mainly divided into two types:
a. Compile time Polymorphism
i. Method Overloading
1. By Changing number of Arguments
2. By Changing Data types of Arguments
ii. Operator Overloading ( ‘+’ Operator )
b. Runtime Polymorphism
i. Method Overriding
21. Compile time Polymorphism
Defn. - Polymorphism that is resolved during compiler time is known as static polymorphism.
♦It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator
overloading.
22. Method Overloading
Defn. -When there are multiple functions with same name but different parameters then these functions are said to be overloaded.
Functions can be overloaded by change in number of arguments or/and change in type of arguments.
1.By Changing number of Arguments 2)By Changing Data
types of Arguments
24. Runtime Polymorphism
Defn. - Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime, thats why it
is called runtime polymorphism. It is also known as Dynamic Method Dispatch.