0% found this document useful (0 votes)
77 views

Lecture 3 - Inheritance - Part 3

This document discusses inheritance in object-oriented programming. It defines inheritance as objects of one class acquiring properties of another class in a hierarchical manner. Subclasses inherit and extend the characteristics of the superclass. The key points covered are: 1) Inheritance allows for hierarchical classification and code reusability. Subclasses add unique properties while inheriting common properties from the superclass. 2) Every class in Java implicitly or explicitly extends the Object class. Subclasses can access all non-private fields and methods of the superclass. 3) Variables can reference subclasses but subclasses cannot reference superclasses without casting. Overriding fields is possible but care must be taken to avoid errors.

Uploaded by

pasan perera
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Lecture 3 - Inheritance - Part 3

This document discusses inheritance in object-oriented programming. It defines inheritance as objects of one class acquiring properties of another class in a hierarchical manner. Subclasses inherit and extend the characteristics of the superclass. The key points covered are: 1) Inheritance allows for hierarchical classification and code reusability. Subclasses add unique properties while inheriting common properties from the superclass. 2) Every class in Java implicitly or explicitly extends the Object class. Subclasses can access all non-private fields and methods of the superclass. 3) Variables can reference subclasses but subclasses cannot reference superclasses without casting. Overriding fields is possible but care must be taken to avoid errors.

Uploaded by

pasan perera
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Advanced Programming

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

class Student extends


Person{ String dit; Student class is a child class
of Person class.
} In Student class we mention
only the attributes and
class Test{ methods which are special
to the child class.
public static void main(String[] arg){
Student s1;
s1 = new Student(); s1.name
Even though an attribute
= “Kamal”; s1.dit = called dit is not mentioned in
“DIT/10/c1/1234”; Student class, a Student
} object inherits that from the
parent.
}
6
Implementing Inheritance in
Java ….
A parent class can
have any number
of child classes
class Employee extends Person{
String dit; WRONG !
}
A child class cannot
have several
class PartTimeStudent extends Student, Employee{ parent classes.
int noOfHours;
}
It is not possible to
class PartTimeStudent extends Student { implement Multiple
int noOfHours; Inheritance in Java.
}

class Test{ Correct !


public static void main(String[] arg){
PartTimeStudent s1; PartTimeStudent inherits every
thing from Person and
s1 = new PartTimeStudent(); Student classes
s1.name = “Kamal”;
s1.dit = “DIT/10/c1/1234”;
7
}
Inheritance Example.
class Test{
public static void main(String[] arg){
Person p; CORRECT !
Student s; PartTimeStudent
t; CORRECT !
A super class variable CAN refer
to a sub class object.
p = new Person();
Because,
p = new Student(); ANY SUB CLASS OBJECT IS A
p = new SUPER CLASS OBJECT (DUE
PartTimeStudent() TO INHERITANCE)
;

s = new Person(); WRONG !


s = new A child class variable
cannot refer to a parent class
PartTimeStudent() object.
;
8
t = new Student();
Inheritance Example.
class Test{
public static void main(String[] arg){ CORRECT !
Person p;
CORRECT !
p = new Student();
WRONG !
p.name = “Kamal”;
p is a variable of type class Person.

p.dit = “DIT/10/c1/1234”; So that we can access only the


attributes and methods mentioned
in Person class using “p.*” .
}
} Even thought variable p refer to a Student
object, that object is treated as a Person object
since the data type of p is Person.
9
Type cast and assignment
 Super class var = subclass object
 It is always acceptable to assign a subclass reference (object) to a
superclass reference (object)
 Sub class var = super class object
 not allowed (additional subclass members not present in the
superclass will remain undefined)
 Sub class var = ( subclass ) super class object
 downcasting is possible only when the superclass object refers to a
subclass object (simply stated, the superclass object is a subclass
object made by previous assignments)
 the operator instanceOf
 Can be used to determines whether an object is referencing a
particular class:
ex. if (point1 instanceOf Circle)

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.

But can access all the attributes


} belongs to Student class using
} variable s.

11
Type casting Example.
class Test{
public static void main(String[] arg){
p
Person p;

p = new Person(); name


Not possible
s
p.name = “Kamal”;
Wrong !
Student s;
Down casting is possible
s = (Student)p;
only when a super class
variable refers to sub class Person Object
s.dit = “DIT/10/c1/1234”; object.

}
} 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).

class Student extends


Person{ String name;
String dit;

public Student(String n, String d){


name = n;
dit = d;
}
}

13
Overriding Data fields ..
 What will be the out put of the following program?

class Test{
public static void main(String[] arg)
{ Student s;

s = new Student(“Kamal”, “3500”);

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

class Student extends


Person{ String name; this.name
String dit; super.nam
e
this.dit
public Student(String n, String d){
this.name = n;
super.name = n+n;
this.dit = d;
}
}

15
Overriding Data fields ..
 What will be the out put of the following program?

class Test{
public static void main(String[] arg)
{ Student s;

s = new Student(“Kamal”, “3500”);

System.out.println(s.name); Kamal
System.out.println(s.dit); 3500
}
}

16
Overriding Data fields ...
display () method

class Student extends Person{


String name;
String dit;

public Student(String n, String n){


this.name = n;
super.name = n+n;
this.dit = d;
}
public void display(){
System.out.println(this.name);
System.out.println(super.name)
; System.out.println(this.dit);
}
}

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.

s = new Student(“Kamal”, “3500”);

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

You might also like