Professional Documents
Culture Documents
Part-001
◌ The process of obtaining the data members and methods from one class to another class is known as inheritance. It
is one of the fundamental features of object-oriented programming
◌ Inheritance is the technique which allows us to inherit the data members and methods from base class to derived
class.
◌ Inheritance is the technique which allows us to inherit the data members and methods from base class to derived
class.
Inheritance
◌ Let us say that the online shopping app wants to sell different types of phones:
◌ Feature phones and Smartphones
Inheritance
◌ The below are the class diagrams for both the classes:
Common attributes & behaviors
◌ We can see that both the class have a lot in common. This is because they both are ultimately phones and each is just
a special type of phone.
Common attributes & behaviors
◌ Since both the classes are of type phone, we can create a phone class with the common attributes and methods and
make these two classes inherit those attributes and behavior, as shown:
Inheritance Advantages
◌ In Java, a child class inherits the parent class using the "extends" keyword. Observe the below code.
class Customer {
//Parent/Super/Base class
}
◌ Let us take one more example. In an organization, we have employees of different designations. Let us consider
Project Manager and System Engineer. All the employees have common attributes like Id, name and email id. So, we
can have a common class called Employee and include these attributes. The Project Manager and System Engineer
classes can inherit the Employee class.
class Employee {
// Parent/Super/Base class
}
◌ By now, you have learnt that the derived classes inherit from the base class. You will now see how the child class
object is created. As you all know, constructors are invoked while creating objects. When a child class object is
created, the child class constructor invokes the parent class constructor before executing any statement present in
the child constructor.
class Customer {
public Customer() {
// 3: Parent constructor will be executed
System.out.println("Creating a customer...");
// 4: The flow will go back to the child constructor
}
}
class RegularCustomer extends Customer {
public RegularCustomer() {
// 2: This constructor will then call the parent constructor
System.out.println("It is a regular customer!");
// 5: The flow will finally come here
}
}
◌ Observe the below code. Since the Manager class is inheriting the Employee class, the Employee class constructor is
invoked by the Manager class constructor.
Constructor call in Inheritance
◌ Problem StatementObserve the code given below. The object of child class is getting created by passing
parameters to the child class constructor but the variables get initialized using the parent class constructor.
Types of Inheritance
◌ There are multiple types of inheritance. We will be discussing some of them in this course.
◌ The first type of inheritance that we will be discussing is Single Inheritance.
◌ In single inheritance, one class is extended by only one class.
◌ You can see in the image given below that only one class, i.e., RegularCustomer extends Customer class.
}
Types of Inheritance
◌ Multiple Inheritance.
◌ In Multiple Inheritance, one class extends multiple classes.
◌ In the below example, Manager is an Employee as well as an Educator. So, Manager class extends both Employee and
Educator class.
Please note that Multiple Inheritance is NOT supported in many object oriented programming languages
including Java.
Polymorphism
◌ Polymorphism is the ability of an object to take different forms, i.e., a single action that can be performed in different
ways. So, polymorphism means many forms.
◌ Consider a simple example of a Bank. All the banks are providing loans to its customers.
◌ But are they all providing it at the same rate?
◌ No, rate of interest may differ from bank to bank. So, object of the Bank class may behave differently when we try to
get the interest rate for bank loans (through a method).
◌ The Bank class, here, shows polymorphism.
Polymorphism
◌ Note: We cannot overload methods by their return type, i.e., two or more methods are not overloaded if they differ
only in their return type.
Dynamic Polymorphism
◌ Similar to static polymorphism or compile time polymorphism, polymorphism can also be achieved at runtime. Such
type of polymorphism is known as dynamic polymorphism. This type of polymorphism is achieved using overriding
the parent method in the child class, called as Method Overriding.
◌ Overriding feature allows the programmer to have a different implementation of parent methods with the same
signature in the child classes. Such parent methods are said to be overridden.
◌ When we override a method in the child class, it should have the same signature as that of the parent class.
◌ The method should not have a weaker access modifier.
◌ Private methods are not overridden.