Académique Documents
Professionnel Documents
Culture Documents
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
superclass:
Person
name:String
dob:Date
Employee
employeeID:int
salary:int
startDate:Date
Person
name="JohnSmith"
dob=Jan13,1954
Employee
name="SallyHalls"
dob=Mar15,1968
employeeID=37518
salary=65000
startDate=Dec15,
2000
Inheritance in Java
Inheritance is declared using the "extends" keyword
publicclassEmployeeextendsPerson
{
privateintemployeID;
privateintsalary;
privateDatestartDate;
[...]
EmployeeanEmployee=newEmployee();
Person
name:String
dob:Date
Employee
employeeID:int
salary:int
startDate:Date
Inheritance Hierarchy
Each Java class has one (and only one) superclass.
Inheritance creates a class hierarchy
Classes higher in the hierarchy are more general and more abstract
Classes lower in the hierarchy are more specific and concrete
Class
Class
Class
Class
Class
Class
Class
Class
Object
Object
Types of Inheritance:
1. Single Inheritance
2. Multiple Inheritance
3. Multi-Level Inheritance
Single Inheritance
When a class is inherited from only one
existing class, then it is single inheritance.
The inherited class is a sub class and the
existing class from which a sub class is
created is the super class.
In single inheritance a super
class can have any number of
sub classes but a sub class can
have only one super class.
Multiple Inheritance
When a class is inherited from
more than one existing class.
The inherited class is a sub class
and all the existing classes from
which the sub class is created are super classes.
Java supports Multiple Inheritance through a
concept called Interface.
Any class that implements an interface is required
to override the methods presented in them.
Multi-Level Inheritance
Multi-Level Inheritance is the extension of
Single Inheritance.
When a class is inherited from another sub class,
then it is Multi-Level Inheritance.
The sub class at the lowest level
can access the member variables
and methods of all the super classes
at the higher level.
Super Class
A new class can be inherited from an
existing class.
The existing class from which a new class is
created is known as the Super class.
It is also called as Base class / Parent
Sub Class
The new class that is inherited from an existing
class is known as Subclass.
This class is also called as Derived class / Child
The subclass inherits all the accessible
methods and member variables of the super
class.
A subclass is created using the extends
keyword.
Sub Class
The new class that is inherited from an existing
class is known as Subclass.
This class is also called as Derived class / Child
The subclass inherits all the accessible
methods and member variables of the super
class.
A subclass is created using the extends
keyword.
Creating Subclass
Syntax
class <class_name> extends <existing_class_name>
{
}
Example
class Cylinder extends Sphere
{
}
An object of the sub class can access the variables and methods of the super class.
Hands-On!
Program Shape.java illustrates how an object of a subclass can access the variables
and methods of a super class.
Constructors - Example
publicclassBankAccount
{
privateStringownersName;
privateintaccountNumber;
privatefloatbalance;
publicBankAccount(intanAccountNumber,StringaName)
{
accountNumber=anAccountNumber;
ownersName=aName;
}
[...]
}
publicclassOverdraftAccountextendsBankAccount
{
privatefloatoverdraftLimit;
publicOverdraftAccount(intanAccountNumber,StringaName,floataLimit)
{
super(anAccountNumber,aName);
overdraftLimit=aLimit;
}
}
Method Overriding
Subclasses inherit all methods from their superclass
anAccount
BankAccount
name="Craig"
accountNumber=123456
account1
OverdraftAccount
name="John"
accountNumber=3323
limit=1000.0
publicfinalbooleanvalidatePassword(Stringusername,StringPassword)
{
[...]
Review
What is inheritance? What is a superclass? What is a
subclass?
Which class is at the top of the class hierarchy in Java?
What are the constructor issues surrounding inheritance?
What is method overriding?
What is a final method? What is a final class?