0% found this document useful (0 votes)
14 views48 pages

Understanding Java Inheritance Concepts

This chapter covers the concept of inheritance in Object-Oriented Programming, detailing its types, benefits, and key Java keywords such as 'super' and 'final'. It explains how inheritance promotes code reusability and hierarchical organization, while also introducing related concepts like aggregation and polymorphism. Additionally, it includes review questions to reinforce understanding of the material.

Uploaded by

whatsrye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views48 pages

Understanding Java Inheritance Concepts

This chapter covers the concept of inheritance in Object-Oriented Programming, detailing its types, benefits, and key Java keywords such as 'super' and 'final'. It explains how inheritance promotes code reusability and hierarchical organization, while also introducing related concepts like aggregation and polymorphism. Additionally, it includes review questions to reinforce understanding of the material.

Uploaded by

whatsrye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object Oriented Programming

CHAPTER- 3
Inheritance
Objectives

After studying this chapter, students should be able


to learn:
 Basics of Inheritance

 Types of Inheritance

 The protected modif ie r

 Super and f inal java key words


 Benef its of inheritance

 Aggregation

 Polymorphism and its types

 Method Overloading vs. Overriding

 The Abstract class

 Review Questions
Inheritance

 Object-oriented programming (OOP) is popular


because:
• It enables reuse of previous code saved as classes
 All Java classes are arranged in a hierarchy
• Object is the superclass of all Java classes
 Inheritance and hierarchical organization capture idea:
• One thing is a ref inement or extension of another
 Reusability - building new classes by utilizing existing
classes.
Inheritance

 It is always good/“productive” if we are able to reuse


something that is
already exists rather than creating the same all over again.
 This is achieved by creating new classes, reusing the data members
and methods of existing classes.
 This mechanism of deriving a new class from existing/old class
is called
“inheritance”.
 The old class is known as “base” class, “super” class or “parent” class
”; and the new class is known as “sub” class, “derived” class, or
“child” class.
Inheritance

Parent

Inherited
capability

Child
Inheritance

superclass = parent class OR base


class
subclass = child class OR derived
class
Example Cont.…
Inheritance
Inheritance

Inheritance relationships often are shown graphically in a


UML class diagram, with an arrow with an open arrow head
pointing to the parent class.
Vehicle

Car

Inheritance should create an is-a relationship, meaning the


child is a
more specif ic version of the parent.
Declaring
Declaringsub
Subclasses
classes

In Java, we use the reserved word extends to establish an


inheritance
relationship

Exampl
e
Declaring sub classes

subclas supercla
s ss
or exten or
ds
derived base
class class
Inheritance - example
Declaring Sub classes
Typesof inheritance

The dif ferent types of inheritances are:


• Single inheritance (only one super class)
• Multiple inheritance (several super classes, not supported by
Java)
• Hierarchical inheritance (one super class, many sub classes)
• Multi-Level inheritance (derived from a derived class)
• Hybrid inheritance (more than two types)
• Multi-path inheritance (inheritance of some properties from two
sources).
Typesof inheritance

A A B A

B C B C D
(a) Single Inheritance (b) Multiple Inheritance (c) Hierarchical Inheritance

A A A

B C B C
B

C D D
(d) Multi-Level Inheritance (e) Hybrid Inheritance (f) Multipath Inheritance
Single inheritance example
Single inheritance example (cont …)
Single inheritance example (cont …)

outp
ut
Multilevel Inheritance example
Multilevel Inheritance example(cont…)

outp
ut
Multiple inheritance

 Multiple inheritance allows a class to be derived from two or


more classes, inheriting the members of all parents

 Collisions, such as the same variable name in two parents,


have to be
resolved

 Java does not support multiple inheritance

 In most cases, the use of interfaces gives us aspects of


multiple
inheritance without the overhead.
The protected modif ier

 Visibility modif iers determine which class members are inherited


and which are not
 Variables and methods declared with public visibility are
inherited; those with private visibility are not
 But public variables violate the principle of encapsulation
 There is a third visibility modif ier that helps in inheritance
situations:
protected
Cont.….

The protected visibility modif ier allows a member of a base class


to be accessed in the child.

• protected visibility provides more encapsulation than


public does
• protected visibility is not as tightly encapsulated as private
visibility
The use of “super” keyword

 “super” is a keyword used to refer to hidden variables of super


class from sub class.
• super.a=a;
 It is used to call a constructor of super class from constructor of
sub class
which should be f irst statement.
• super(a,b);
 It is used to call a super class method from sub class method to
avoid redundancy of code
• [Link](a, b);
The use of “super” keyword

 Why is super needed to access super-class members?


 When a sub-class declares the variables or methods with the same
names and types as its super-class:

 The re-declared variables/methods hide those of the


The use of “super” keyword
The use of “super” keyword

 Although the i variable in B hides the i variable in A, super


allows
access to the hidden variable of the super-class:
Using f inal keywordwith Inheritance

 f inal keyword is used to declare constants which can not


change its
value of definition.

 f inal variables can not change its value.

 f inal methods can not be Overridden.

 f inal Classes can not be extended or inherited


Preventing Overriding with f inal

 A method declared f inal cannot be overridden in any


sub-class:
Preventing Overriding with f inal

 A class declared f inal cannot be inherited and has no sub


-classes. f inal class A { … }
 This class declaration is considered illegal:
class B extends A { … }
 Declaring a class final implicitly declares all its methods
final.
The Benef its of Inheritance

 Code reusability:- Inheritance automates the process of


reusing the code of the super classes in the subclasses.
 With inheritance, an object can inherit its more general

properties from its parent object, and that saves the


redundancy in programming.
 Code maintenance:- Organizing code into hierarchical classes
makes
its maintenance and management easier.
The Benef its of Inheritance

 Implementing OOP:- Inheritance helps to implement the


basic OOP philosophy to adapt computing to the problem
and not the other way around, because entities (objects) in
the real world are often organized into a hierarchy.
 Increased Reliability :-(resulting from reuse and sharing of
well- tested code)
Aggregation

 Aggregation in Java is a relationship between two classes that


is best
described as a "has-a" and "whole/part" relationship.
 It is a more specialized version of the association
relationship. The aggregate class contains a reference to
another class and is said to have ownership of that class.
 Each class referenced is considered to be part-of the
aggregate class.
 For example, if you imagine that a Student class that stores
information about individual students at a school. Now
assume a Subject class that holds the details about a
particular subject
Aggregation

 If the Student class is def ined to contain a Subject object


then it can be said that the Student object has-a Subject
object.
 The Subject object also makes up part-of the Student object
after all, there is no student without a subject to study.
The Student object, therefore, owns the Subject object.
Aggregation
Polymorphism

 Polymorphism came from the two Greek words „poly‟means


many and
morph means forms i.e. many forms
 If the same method has ability to take more than one form to
perform several tasks then it is called polymorphism.
 A polymorphic reference is a variable that can refer to dif ferent
types of
objects at dif ferent points in time.
 It is of two types: Static Polymorphism and Dynamic
Polymorphism.
Static Polymorphism

 Static Polymorphism:
 The polymorphism exhibited at compile time is called

Static polymorphism.
 Here the compiler knows which method is called at the

compilation. This is also called compile time


polymorphism or static binding.
 Achieving method overloading & method overriding

using private, static and f inal methods is an example


of Static Polymorphism.
Static Polymorphism
Dynamic Polymorphism

 The polymorphism exhibited at run time is called dynamic


polymorphism.
 In this dynamic polymorphism a method call is linked with
method
body at the time of execution by JVM.
 Java compiler does not know which method is called at the
time of compilation. This is also known as dynamic binding
or run time polymorphism.
 Method overloading and method overriding are examples of
Dynamic Polymorphism in Java. Provided that the methods
are instance method.
Dynamic Polymorphism
Dynamic Polymorphism using method overriding

Writing two or more methods in super & sub classes with same name
and same
signatures is called method overriding.
In method overriding JVM executes a method depending on the type
of the object.
Dynamic Polymorphism using method overriding
Overloading Vs Overriding

 Overloading deals with  Overriding deals with two


multiple methods in the same methods, one in a parent class
class with the same name but and the other one in a child class,
dif ferent signatures with the same method name
and signatures/parameters.
 Overloading lets you def ine
 Overriding lets you def ine a similar
a similar operation in different
operation in dif ferent ways for
ways
dif ferent object types
for dif ferent data.
Abstract class

 A method with method body is called concrete method. In


general any
class will have all concrete methods.
 A method without method body is called abstract method. A
class that contains abstract method is called abstract class. i.e.
An abstract class is a class with zero or more abstract methods
 It is possible to implement the abstract methods dif ferently in
the subclasses of an abstract class.
Abstract class

 These dif ferent implementations will help the programmer to


perform
dif ferent tasks depending on the need of the sub classes.
 Moreover, the common members of the abstract class are also
shared by
the sub classes.
 The abstract methods and abstract class should be declared
using the keyword abstract.
 We cannot create objects to abstract class because it is having
incomplete code.
Abstract class

 Whenever an abstract class is created, subclass should be


created to it and the abstract methods should be
implemented in the subclasses, then we can create objects
to the subclasses.
 Abstract class reference can be used to refer to the objects
of its sub classes.
 Abstract class references cannot refer to the individual
methods of sub classes.
 A class cannot be both „abstract‟ & „f inal‟simultaneously
 So, it is illegal to declare a class as both abstract and final.
Why?
Abstract class
Abstract class
Review Questions

1. What is inheritance?
2. What is the purpose of super keyword?
3. Which class is at the top of the class hierarchy in Java?
4. What are the constructor issues surrounding
inheritance?
5. What is method overriding?
6. Using aggregation, show the relationship between
Address and Employee class.
7. Describe the two types of polymorphisms with
example
8. What is the purpose of “f inal” java key word
ThankYou!

You might also like