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

OOP-Chapter 3

The document discusses inheritance in object-oriented programming. It defines inheritance and the key concepts of superclasses, subclasses, overriding methods, and using this() and super(). It provides examples of single inheritance, method overriding, and accessing protected members inherited from the superclass.

Uploaded by

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

OOP-Chapter 3

The document discusses inheritance in object-oriented programming. It defines inheritance and the key concepts of superclasses, subclasses, overriding methods, and using this() and super(). It provides examples of single inheritance, method overriding, and accessing protected members inherited from the superclass.

Uploaded by

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

1

INHERITANCE

4/6/2024 OOP- CH-3- Inheritance


Contents
1. Introduction
2. Super-classes and sub-classes
3. Protected members
4. Overriding methods
5. Using this() and super()
6. Use of final keyword with inheritance
7. Constructors in subclasses

4/6/2024
Contents
1. Introduction
2. Super-classes and sub-classes
3. Protected members
4. Overriding methods
5. Using this() and super()
6. Use of final keyword with inheritance
7. Constructors in subclasses

4/6/2024
Introduction
4

 Inheritance is one of the key features of OOP that


allows us to create a new class from an existing
class.
 Itis the ability to derive something specific from
something generic.
 A class can inherit the futures of another class and
add its own modification
 The existing class is called the parent class, or
superclass, or base class
 The derived class is called the child class or subclass

4/6/2024
5

 As the name implies, the child inherits


characteristics of the parent
 Thatis, the child class (subclass) inherits all the
properties and methods of the parent (super) class
 Proper inheritance creates an is-a relationship,
meaning the child is a more specific version of the
parent
Vehicle

Car

4/6/2024
Is-a relationship
6

 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

 Here, Car can inherit from Vehicle, Orange can


inherit from Fruit, and so on.
4/6/2024
Why use Inheritance?
7

 The most important use of inheritance in Java is


code reusability.
 The code that is present in the parent class can be
directly used by the child class.
 Method overriding is also known as runtime
polymorphism.
 Hence, we can achieve Polymorphism in Java with the
help of inheritance.

4/6/2024
Type of Inheritance
8

 There are five types of inheritance.


 Single inheritance
 Multiple inheritance

 Hierarchical inheritance

 Multilevel inheritance

4/6/2024
9

 Single Inheritance
 In single inheritance, a single subclass extends from a
single superclass.
 For example,

4/6/2024
10

 Multilevel Inheritance
 Inmultilevel inheritance, a subclass extends from a
superclass and then the same subclass acts as a
superclass for another class.
 For example

4/6/2024
11

 Hierarchical Inheritance
 In hierarchical inheritance, multiple subclasses extend
from a single superclass.
 For example,

4/6/2024
12

 Multiple Inheritance
 Inmultiple inheritance, a single subclass extends from
multiple super classes.
 Note: Java doesn't support multiple inheritance

 For example,

4/6/2024
13

 Hybrid Inheritance
 Hybrid inheritance is a combination of two or more
types of inheritance.
 For example,

4/6/2024
Contents
1. Introduction
2. Super-classes and sub-classes
3. Protected members
4. Overriding methods
5. Using this() and super()
6. Use of final keyword with inheritance
7. Constructors in subclasses

4/6/2024
Super and Sub Classes
15

 The aim of inheritance is to provide the reusability of


code so that
 A class has to write only the unique features and
 Rest of the common properties and functionalities can be
extended from the another class.
 Sub class
 The class that extends the features of another class is
known as child class, sub class or derived class.
 Super class
 The class whose properties and functionalities are
used(inherited) by another class is known as parent class,
super class or Base class.
4/6/2024
extends keyword
16

 The extends keyword is used to perform inheritance


in Java.
 Here class Car is child class and class Vehicle is parent
class.
 The class Car is inheriting the properties and methods
of Vehicle class.
class Car extends Vehicle
{

}
4/6/2024
Example
17

 Classes in Java support single inheritance; the Car


class can't extend multiple classes.
 Also, note that in the absence of an extends keyword, a
class implicitly inherits class java.lang.Object.
4/6/2024
18

 A subclass class inherits the protected and public


members from the superclass class.
 In addition, the members with default and package access
are inherited if the two classes are in the same package.
 On the other hand, the private members of a class are
not inherited.
 To access inherited properties or methods, we can
simply use them directly:
 Note that we don't need a reference to the superclass to
access its members

4/6/2024
Contents
1. Introduction
2. Super-classes and sub-classes
3. Protected members
4. Overriding methods
5. Using this() and super()
6. Use of final keyword with inheritance
7. Constructors in subclasses

4/6/2024
Protected Members
20

 A class's public members are accessible wherever


the program has a reference to an object of that
class or one of its subclasses.
 A class's private members are accessible only from
within the class itself. A superclass's private
members are not inherited by its subclasses.
 Using protected access offers an intermediate level
of access between public and private.

4/6/2024
21

A superclass's protected members can be accessed by


members of that superclass, by members of its
subclasses and by members of other classes in the same
package (i.e., protected members also have package
access).
 All public and protected superclass members retain
their original access modifier when they become
members of the subclass.
 public
members of the superclass
become public members of the subclass, and

4/6/2024
22

 protectedmembers of the superclass


become protected members of the subclass).
 Subclass methods can refer to public and protected
members inherited from the superclass simply by
using the member names.

4/6/2024
23 4/6/2024
Contents
1. Introduction
2. Super-classes and sub-classes
3. Protected members
4. Overriding methods
5. Using this() and super()
6. Use of final keyword with inheritance
7. Constructors in subclasses

4/6/2024
Inheritance and Method Overriding
25

 When we declare the same method in child class


which is already present in the parent class the
this is called method overriding.
 In this case when we call the method from child class
object, the child class version of the method is called.
 However we can call the parent class method using
super keyword.

4/6/2024
26

 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

 Rules for Java Method Overriding


 The method must have the same name as in the parent class
 The method must have the same parameter as in the parent
class.
 There must be an IS-A relationship (inheritance).

4/6/2024
27

 Example:

4/6/2024
28

 Method overriding properties


 Overriding and Access-Modifiers : The access modifier
for an overriding method can allow more, but not less,
access than the overridden method.
 Final methods can not be overridden : If we don’t
want a method to be overridden, we declare it as final.
 Static methods can not be overridden: When you
define a static method with same signature as a static
method in base class, it is known as method hiding.

4/6/2024
29

 Privatemethods can not be overridden : Private


methods cannot be overridden as they are bonded
during compile time. Therefore we can’t even override
private methods in a subclass.

4/6/2024
30

 Invoking overridden method from sub-class : We can


call parent class method in overriding method
using super keyword.
 Overriding and constructor : We can not override
constructor as parent and child class can never have
constructor with same name(Constructor name must
always be same as Class name).

4/6/2024
Contents
1. Introduction
2. Super-classes and sub-classes
3. Protected members
4. Overriding methods
5. Using this and super keyword
6. Use of final keyword with inheritance
7. Constructors in subclasses

4/6/2024
Using this and super keyword
32

 Java provides two useful implicit reference


variables ‘this’ and ‘super’.
 Both are used to resemble the reference variable
pointing to the super class and subclass

4/6/2024
33

 this Keyword
 It is an implicit reference variable that is used to refer
current class objects
 It invokes the constructor of subclass
 In the case of overridden methods, this keyword is used
to invoke the method of subclass
 It solves the problem of variable hiding. Using this
keyword, a compiler always refers to instance variable
of the current class.
 It can be used to return and pass as an argument in
context of current class object
4/6/2024
34

 super keyword
 It is an implicit reference variable that is used to refer
child class’s immediate parent class objects.
 It invokes the constructor of superclass

 In the case of the overridden method, super keyword is


used to invoke the method of superclass.
 Using super keyword, compiler always refer to
immediate parent class instance variable.
 It can be used to return and pass as an argument in
context of parent class object.
4/6/2024
Java Inheritance Hiding(Shadowing)
35

 Variable Hiding
 When both the child and the parent classes have a
variable with the same name, the child's variable
hides the one from the parent.

4/6/2024
36 4/6/2024
37

 To test it, let's initialize two instances. One with


parent class and another with the child, then invoke
the printInstanceVariable() methods on each of them:

4/6/2024
38

 Method Hiding
 Method hiding may happen in any hierarchy structure in
java.
 When a child class defines a static method with the
same signature as a static method in the parent class,
then the child's method hides the one in the parent class.
 The same behavior involving the instance methods is
called method overriding.

4/6/2024
39 4/6/2024
40

 Method Hiding vs Overriding


 Hiding doesn't work like overriding, because static
methods are not polymorphic.
 Overriding occurs only with instance methods.
◼ It
supports late binding, so which method will be called is
determined at runtime.
 On the other hand, method hiding works with static
ones. Therefore it's determined at compile time.

4/6/2024
Contents
1. Introduction
2. Super-classes and sub-classes
3. Protected members
4. Overriding methods
5. Using this() and super()
6. Use of final keyword with inheritance
7. Constructors in subclasses

4/6/2024
final keyword
42

 Java final keyword is a non-access specifier that is


used to restrict a class, variable, and method.
 Declaring constants (used with variable and argument
declaration)
◼ final int MAX=100;
 Disallowing method overriding (used with method
declaration)
◼ final void show (final int x)
 Disallowing inheritance (used with class declaration).
◼ final class Demo {}
4/6/2024
43

 Example
class Parent {
final void final_method() {
//definition of the Final Method
}
}
class Child extends Parent {
final void final_method(){
//generates a compile-time error
}
}

4/6/2024
Contents
1. Introduction
2. Super-classes and sub-classes
3. Protected members
4. Overriding methods
5. Using this() and super()
6. Use of final keyword with inheritance
7. Constructors in subclasses

4/6/2024
Constructor in subclass
45

 A class that extends another class does not inherit


its constructors.
 However, the subclass must call a constructor in the
superclass inside of its the subclass constructors!
 If a subclass calls another constructor within itself, then
the called constructor must call the superclass
constructor.
 To invoke a superclass's constructor, use
the super keyword

4/6/2024
46

 Invocation of a superclass constructor must be the first


line in the subclass constructor.
 The syntax for calling a superclass constructor is
◼ super();
◼ the superclass no-argument constructor is called.
or:
◼ super(parameter list);
◼ the superclass constructor with a matching parameter list is
called.

4/6/2024
47

 Example
public Box()
{
super();
height = 0;
}

public Box(double l, double w, double h)


{
super(l, w);
height = h;
}

4/6/2024
48

Questions?
4/6/2024
49

Thank You
4/6/2024

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy