Inheritance
Presented by
Ch.v.Durga Prasad
Inheritance
The main objectives of inheritence are:
To explore the concept of inheritance
To define the syntax of inheritance in Java
To understand the class hierarchy of Java
Terminology
Inheritance is a fundamental Object Oriented concept
A class can be defined as a "subclass" of another class.
The subclass inherits all data attributes of its superclass
The subclass inherits all methods of its superclass
The subclass inherits all associations of its superclass
The subclass can: superclass: Person
- name: String
Add new functionality - dob: Date
Use inherited functionality
Override inherited functionality
subclass: Employee
- employeeID: int
- salary: int
- startDate: Date
What really happens?
When an object is created using new, the system must
allocate enough memory to hold all its instance variables.
This includes any inherited instance variables
In this example, we can say that an Employee "is a kind of"
Person.
An Employee object inherits all of the attributes, methods and
associations of Person
Person Person
- name: String name = "John Smith"
- dob: Date dob = Jan 13, 1954
Employee
name = "Sally Halls"
is a kind of
dob = Mar 15, 1968
Employee employeeID = 37518
- employeeID: int salary = 65000
- salary: int startDate = Dec 15,
- startDate: Date 2000
Inheritance in Java
Inheritance is declared using the "extends" keyword
If inheritance is not defined, the class extends a class called Object
public class Person
{ Person
private String name;
- name: String
- dob: Date
private Date dob;
[...]
public class Employee extends Person
{ Employee
private int employeID; - employeeID: int
private int salary; - salary: int
private Date startDate; - startDate: Date
[...]
Employee anEmployee = new Employee();
Inheritance Hierarchy
Each Java class has one (and only one) superclass.
• Java does not allows for multiple inheritance
• C++,Python allows for multiple inheritance.
Inheritance creates a class hierarchy
There is no limit to the multiple Inheritencce
number of subclasses a
class can have
There is no limit to the
depth of the class tree.