You are on page 1of 22

Abstraction in Java

Part-001

Copyright © 2021 Infogain Corporation. All Rights Reserved.


Objectives
◌ Comparison of various programming techniques
◌ Introduction to Object Oriented Concepts
◌ What is an Object
◌ Abstraction, Encapsulation, Message Passing
◌ Class, Access Specifiers, Examples
◌ Advanced Object-Oriented Concepts
◌ Relationships
◌ Inheritance
◌ Abstract Classes and Polymorphism
◌ Object Oriented Design Methodology
◌ Trends in OO Technology
Inheritance

◌ 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

◌ There are three main advantages of inheritance:


◌ We can keep common properties in a single place. Thus any changes needs to be made need not be repeated.
◌ Inheritance encourages code reuse thus saving us time.
◌ If we want to add a new type of phone later on, we can simply inherit the Phone class instead of writing it from
scratch.
Inheritance extends keyword

◌ In Java, a child class inherits the parent class using the "extends" keyword. Observe the below code.

class Customer {
//Parent/Super/Base class
}

class RegularCustomer extends Customer { // RegularCustomer is a Customer


//Child/Sub/Derived class
}

class Guest extends Customer { // Guest is a Customer


//Child/Sub/Derived class
}
Inheritance extends keyword

◌ 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
}

class ProjectManager extends Employee { // ProjectManager is an Employee


// Child/Sub/Derived class
}

class SystemEngineer extends Employee { // SystemEngineer is an Employee


// Child/Sub/Derived class
}
Constructor call in Inheritance

◌ 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
}
}

public class Tester {


public static void main(String[] args) {
RegularCustomer regularCustomer = new RegularCustomer();
// 1: This line will be executed first and the flow will go to [2]
}
}
Constructor call in Inheritance

◌ 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.

public class Customer {


// Parent/Super/Base class
}
public class RegularCustomer extends Customer { //
RegularCustomer is a Customer
// Child/Sub/Derived class
}
Types of Inheritance

◌ The next type of inheritance that we will be discussing is Multilevel inheritance.


◌ In multilevel inheritance, a class extends another class which extends another class.
◌ SwiftFood app has one more category of customers - PremiumCustomers. PremiumCustomers are special type of
RegularCustomers who are provided with reward points apart from getting discount.
◌ In this case, the RegularCustomer class extends the Customer class and the PremiumCustomer class extends
the RegularCustomer class. This is an example of Multilevel inheritance. 

public class Customer {


//code
}
public class RegularCustomer extends Customer {
//code
}
public class PremiumCustomer extends RegularCustomer {
//code

}
Types of Inheritance

◌ One more type of Inheritance is Hierarchical Inheritance. 


◌ When more than one class extends the same base class, then that type of inheritance is said to be Hierarchical
Inheritance.
◌ Here, the Customer class is the parent class and the RegularCustomer and Guest classes extend the Customer class.
This is an example of Hierarchical Inheritance.

public class Customer {


// code here
}
public class RegularCustomer extends Customer {
// code here
}
public class Guest extends Customer {
// code here
}
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

◌ Polymorphishm is of two types:


◌ Static polymorphism
◌ Dynamic polymorphism
◌ Polymorphism that gets resolved during compile time is known as static polymorphism or compile time
polymorphism. This polymorphism is achieved using overloading of the methods in the same class, called as Method
overloading.
◌ Method overloading allows the programmer to have multiple methods with the same name in the same class, but
differing in their signature.
◌ Signature can differ by
◌ the number of parameters
◌ the data type of parameters
◌ the order of the parameters

◌ 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.

You might also like