OOP Concepts Part 2 - Inheritance
OOP Concepts Part 2 - Inheritance
OOP Concepts Part 2 - Inheritance
Programming Features
Part 2
Inheritance
Learning Objectives
In this lesson, you will learn to:
◦ understand the concept of software reuse
◦ distinguish the types of inheritance
◦ explain the benefits of using inheritance
◦ apply inheritance in a Java application
◦ identify the rules and restrictions in using
inheritance
Inheritance
Inheritance allows child classes to inherit the
characteristics of an existing parent class
◦ Attributes (fields and properties)
◦ Operations (methods)
Child class can extend the parent class
◦ Add new fields and methods
◦ Redefine methods (modify existing behavior)
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.
Types of Inheritance
Inheritance terminology
Inheritance – Benefits
Inheritance has a lot of benefits
◦ Extensibility
◦ Reusability
◦ Provides abstraction
◦ Eliminates redundant code
Use inheritance for building “is-a”
relationships
◦ E.g. dog is-a animal (dogs are kind of animals)
Don't use it to build “has-a” relationship
◦ E.g. dog has-a name (dog is not kind of name)
Inheritance – Example
Types of Inheritance
characteristics are
specified high up in
the hierarchy
More specific
characteristics are
specified below the
hierarchy
Inheritance in Java
A class can inherit only one base class
◦ Example: public class B extends A
super(x);
...
}
Simple Inheritance Example
public class Mammal public class Dog extends Mammal
{ {
public int age; public String breed;