0% found this document useful (0 votes)
23 views

5 Inheritance in JAVA

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

5 Inheritance in JAVA

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Inheritance in JAVA

INTRODUCTION TO JAVA PROGRAMMING

CSA6003T
BHAWANA BOTHARA
ASSISTANT PROFESSOR
Learning Objective
 Understand the concept of inheritance in Java.
 Differentiate between different types of inheritance.
 Implement inheritance in Java programs.
 Recognize the benefits and limitations of inheritance.
 Apply inheritance in real-world scenarios effectively.
Introduction to Inheritance
 Inheritance is a mechanism in Java where one class acquires the properties (fields) and
behaviours (methods) of another class.
 It promotes code reuse and establishes a parent-child relationship between classes.
 In Java, inheritance is an is-a relationship. That is, we use inheritance only if there exists
an is-a relationship between two classes. For example,
 Car is a Vehicle
 Orange is a Fruit
 Surgeon is a Doctor
 Dog is an Animal
The syntax of Java Inheritance

class Subclass-name extends Superclass-name


{
//methods and fields
}
class Employee
{
float salary=40000;
}
class Programmer extends Employee
{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Member Access and Inheritance

 Although a subclass includes all of the members of its superclass, it cannot access
those members of the superclass that have been declared as private.
Java Inheritance Types
 Below are the different types of inheritance which are supported by Java.
 Single Inheritance
 Multilevel Inheritance
 Hierarchical Inheritance
 Multiple Inheritance
 Hybrid Inheritance
Single Inheritance
 In single inheritance, subclasses inherit the features of one superclass. In the
image below, class A serves as a base class for the derived class B.
Multilevel Inheritance
 In Multilevel Inheritance, a derived class will be inheriting a base class, and as
well as the derived class also acts as the base class for other classes. In the below
image, class A serves as a base class for the derived class B, which in turn serves
as a base class for the derived class C. In Java, a class cannot directly access the
grandparent’s members.
Hierarchical Inheritance
 In Hierarchical Inheritance, one class serves as a superclass (base class) for more
than one subclass. In the below image, class A serves as a base class for the
derived classes B, C, and D.
Multiple Inheritance (Through
Interfaces)
 In Multiple inheritances, one class can have more than one superclass and inherit features from
all parent classes. Please note that Java does not support multiple inheritances with classes. In
Java, we can achieve multiple inheritances only through Interfaces.
Hybrid Inheritance
 It is a mix of two or more of the above types of inheritance. Since Java doesn’t support multiple
inheritances with classes, hybrid inheritance involving multiple inheritance is also not possible with
classes. In Java, we can achieve hybrid inheritance only through Interfaces if we want to involve
multiple inheritance to implement Hybrid inheritance.
Access Specifiers
 public: Accessible from any other class.
 private: Accessible only within the declared class.
 protected: Accessible within the same package and subclasses.
 default: Accessible only within the same package.
Method Overriding in Java Inheritance

 if the same method is present in both the superclass and subclass, what will
happen?
 In this case, the method in the subclass overrides the method in the superclass.
This concept is known as method overriding in Java.
 If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in Java.
 In other words, If a subclass provides the specific implementation of the method
that has been declared by one of its parent class, it is known as method overriding.
Usage of Java Method Overriding

 Method overriding is used to provide the specific implementation of a method


which is already provided by its superclass.
 Method overriding is used for runtime polymorphism

 Can we override static method?


 No, a static method cannot be overridden.
//Creating a parent class.
class Vehicle{
//defining a method
void run(){System.out.println("Vehicle is running");}
}
//Creating a child class
class Bike2 extends Vehicle{
//defining the same method as in the parent class
void run(){System.out.println("Bike is running safely");}

public static void main(String args[]){


Bike2 obj = new Bike2();//creating object
obj.run();//calling method
}
}
A real example of Java Method Overriding
S.no. Method Overloading Method Overriding
1 Method overloading is used to increase the Method overriding is used to provide the specific
readability of the program. implementation of the method that is already
provided by its super class.

2 Method overloading is performed within class. Method overriding occurs in two classes that have
IS-A (inheritance) relationship.

3 In case of method overloading, parameter must be In case of method overriding, parameter must be
different. same.

4 Method overloading is the example of compile time Method overriding is the example of run time
polymorphism. polymorphism.

5 In java, method overloading can't be performed by Return type must be same or covariant in method
changing return type of the method only. Return type overriding.
can be same or different in method overloading. But
you must have to change the parameter.
super Keyword in Java Inheritance

 Previously we saw that the same method in the subclass overrides the method in
superclass.
 In such a situation, the super keyword is used to call the method of the parent
class from the method of the child class.
 You can call the superclass's method from within the subclass using
super.methodName().
 Additionally, super() is used to call the constructor of the superclass from the
subclass constructor, which is essential for initializing inherited members.
Using super to Call Superclass
Constructors
 A subclass can call a constructor defined by its superclass by use of the following
form of super:

super(arg-list);

 Here, arg-list specifies any arguments needed by the constructor in the superclass.
super( ) must always be the first statement executed inside a subclass’ constructor.
A Second Use for super

 The second form of super acts somewhat like this, except that it always refers to
the superclass of the subclass in which it is used. This usage has the following
general form:

super.member

 Here, member can be either a method or an instance variable


Review and Summary

 In Java, it is possible to inherit attributes and methods from one class to another.
We group the "inheritance concept" into two categories:

 subclass (child) - the class that inherits from another class


 superclass (parent) - the class being inherited from
 To inherit from a class, use the extends keyword.
Assessment
1. Which keyword is used to inherit a class in Java?
a) implement
b) extends
c) inherit
d) super
2. Inheritance promotes __________ by allowing code reuse. Answer: code reusability
3. Which of the following is not a type of inheritance supported by Java?
a) Single Inheritance
b) Multilevel Inheritance
c) Hierarchical Inheritance
d) Multiple Inheritance
Assessment

4. The method in a subclass that provides a specific implementation of a method


already defined in its superclass is called __________. Answer: method overriding
5. What do you use to access overridden method of parent class? Super
Assignment

1. What is Inheritance Java?


2. What are the 4 types of inheritance in Java?
3. What is the use of extend keyword?
4. What is super () and super keyword in Java?
5. Explain the difference between method overloading and method overriding
with examples.
6. What is the significance of the super keyword in inheritance?

You might also like