Lecture 3 - Inheritance - Part 3
Lecture 3 - Inheritance - Part 3
Inheritance
Lecture 3
Inheritance
Inheritance is the process by which
objects of one class acquire the
properties of objects of another
class.
Inheritance supports the concept of
hierarchical classification.
For example, the bird robin is a
part of the class flying bird, which is
again a part of the class bird.
The principle behind this sort of
division is that each derived class
shares common characteristics
with the class from which it is
derived
2
Inheritance ….
Organizes objects in a top-down fashion from most general to
least general
Inheritance vs. Composition
Inheritance defines a “is-a” relationship
A mountain bike “is a” kind of bicycle
A SUV “is a” kind of automobile
A border collie “is a” kind of dog
A laptop “is a” kind of computer
Composition is “has-a” relationship (a mammal has a leg)
A form of software reusability in which a new class is created
from an existing class by absorbing its attributes and behaviors
The new class adds unique characteristics which do not belong
to the existing class.
3
Object-oriented terminology
In object-oriented programming languages, a class created b y
extending another class is called a subclass.
The class used for the basis is called the superclass
superclass – the existing, previously defined class, the
inherited one.
subclass – the new class which inherits the superclass
extends keyword – subclass extends superclass
Every subclass object is an object of the superclass as well
“subclass-object-is-a-superclass-object” relationship (but not
vice-versa!)
A subclass object is generally larger than the superclass
objects.
Alternative terminology
The superclass is also referred to as the base or parent
class
The subclass is also referred to as the derived or child class
4
Java’s Mother of all objects
Class Object
Every new class MUST extend another
class.
If such is not specified, Java implicitly
extends Object.
Object is the super class of the entire
Java class hierarchy.
5
Implementing Inheritance in
Java
Person class is a
child class of class
Object.
class Person{
String name; Keyword extends
Allows us to implement
} Inheritance
10
Type casting Example.
class Test{
public static void main(String[] arg){
p
Person p;
p = new Student();
name
s dit = “..”
p.name = “Kamal”;
Down casting.
Student s;
Both variables p & s refer to
the same Student object.
s = (Student)p;
Can access only the attributes
mentioned in Person class using Student Object
s.dit = “DIT/10/c1/1234”; variable p.
11
Type casting Example.
class Test{
public static void main(String[] arg){
p
Person p;
}
} Gets a runtime error.
12
Overriding Data fields
A superclass’s instance variable can be hidden by a subclass’s def inition of
an instance variable with the same name (override).
13
Overriding Data fields ..
What will be the out put of the following program?
class Test{
public static void main(String[] arg)
{ Student s;
System.out.println(s.name); Kamal
System.out.println(s.dit); 3500
}
}
14
Overriding Data fields
Student class has two attributes with the same name.
One inherits from parent.
The other overrided in the child class. Student Object
15
Overriding Data fields ..
What will be the out put of the following program?
class Test{
public static void main(String[] arg)
{ Student s;
System.out.println(s.name); Kamal
System.out.println(s.dit); 3500
}
}
16
Overriding Data fields ...
display () method
17
Overriding Data fields ..
What will be the out put of the following program?
Outsiders (class Test)
class Test{ can access only the
public static void main(String[] arg) attributes belongs to the
{ Student s; child.
System.out.println(s.name); Kamal
System.out.println(s.dit); 3500
Only the child can
s.display(); Kamal access both
} KamalKamal attributes.
} 3500
18
Finality
Classes, methods and attributes can be declared a s
final.
A final class
Cannot be inherited (cannot create child classes).
MUST be a concrete class. (must be able to create objects)
All the attributes and methods in the class are automatically
final.
A Final method
Any child class cannot override it.
Must have a method implementation. (a method body)
(Must be able to invoke/call the method)
A final attribute
Any child class cannot override it.
Must initialize as soon as we declare the variable.
Cannot change the value later.
19
final attributes
Final attributes are instance variables.
Each object will keep a separate copy of that
attribute.
Attributes MUST be declared as final static
to behave as constants.
If you declare an attribute as static only.
One copy will be maintained for the whole class.
Can be accessed with objects or without
objects (<class name>.attribute)
Can change the value at any time.
20
OOP Contd.
End – Lecture 6