it203 oop introduction to oop sjp art - …...2015/03/03 · java programming: oop 62 inheritance...
TRANSCRIPT
![Page 1: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/1.jpg)
Inheritance
61
![Page 2: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/2.jpg)
Java Programming: OOP 62
Inheritance
• On the surface, inheritance is a code re-use issue.
– we can extend code that is already written in a
manageable manner.
• Inheritance is more, it supports polymorphism at the
language level!
• Information is made manageable in a hierarchical
order
![Page 3: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/3.jpg)
Inheritance cont.
• With Inheritance, new class can be derived from
existing classes as a building block
• New class Inherit properties and methods from the
existing class
• New class can also added Its own properties and
methods
• Keyword
– Extends
– Implements
63
![Page 4: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/4.jpg)
Java Programming: OOP 64
Inheritance cont.
• Take an existing object type (collection of fields and methods) and extend it.
– create a special version of the code without re-writing any of the existing code (or even explicitly calling it!).
– End result is a more specific object type, called the sub-class / derived class / child class.
– The original code is called the super class / parent class / base class.
![Page 5: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/5.jpg)
Example
7/11/2015 Budditha Hettige ([email protected]) 65
Student
University
Student
Undergraduate Post graduate
Employee
Phone
Employee
-----------------
-
Name
Phone
FulltimeEmployee
------------------------
Salary
Office
Manager
Manager
------------------
CompanyCar
Phone
Contractor
------------------
HourlyRate,
ContractDuration
![Page 6: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/6.jpg)
66
Introduction (Cont.)
• Class hierarchy
– Direct superclass
• Inherited explicitly (one level up hierarchy)
– Indirect superclass
• Inherited two or more levels up hierarchy
– Single inheritance
• Inherits from one superclass
– Multiple inheritance
• Inherits from multiple superclasses
![Page 7: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/7.jpg)
Inheritance
67
Animal
Dog
public class Animal
{
}
public class Dog extends Animal
{ }
![Page 8: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/8.jpg)
Introduction (Cont.)
68
Animal
Dog
Animal
Dog
Private
Public
Public
![Page 9: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/9.jpg)
69
protectedprotectedprotectedprotected Members
• protected access
– Intermediate level of protection between public and private
– protected members accessible by
• superclass members
• subclass members
• Class members in the same package
– Subclass access to superclass member
• Keyword super and a dot (.)
![Page 10: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/10.jpg)
Introduction (Cont.)
70
Animal
Dog Animal
Dog
Private
Public
Public
Protected
Protected
![Page 11: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/11.jpg)
Example
71
![Page 12: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/12.jpg)
IS-A Relationship (Example)
public class Animal
{
}
public class Mammal extends Animal
{ }
public class Reptile extends Animal
{ }
public class Dog extends Animal
{ }
72
Animal
Mamal
extends
![Page 13: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/13.jpg)
IS-A Relationship (Example)
public class Dog extends Mammal
{
public static void main(String args[])
{
Animal a = new Animal();
Reptile r = new Reptile();
Mammal m = new Mammal();
Dog d = new Dog();
System.out.println(m instanceof Animal);
System.out.println(d instanceof Mammal);
System.out.println(d instanceof Animal);
}
}
73
Animal
Mamal Dog Reptile
![Page 14: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/14.jpg)
IS-A Relationship
• In Object Oriented terms following are true:
– Animal is the superclass of Mammal class.
– Animal is the superclass of Reptile class.
– Mammal and Reptile are sub classes of Animal
class.
– Dog is the subclass of both Mammal and Animal
classes.
74
![Page 15: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/15.jpg)
IS-A Relationship
• IS-A relationship we can say:
– Mammal IS-A Animal
– Reptile IS-A Animal
– Dog IS-A Mammal
– Hence : Dog IS-A Animal as well
• Use of the extends keyword the subclasses will be
able to inherit all the properties of the superclass
except for the private properties of the superclass
75
![Page 16: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/16.jpg)
Java Programming: OOP 76
Accessing super class methods
• Can use super() to access all (non-private) super
class methods.
– even those replaced with new versions in the
derived class.
• Can use super() to call base class constructor.
![Page 17: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/17.jpg)
Java Programming: OOP 77
Single Inheritance
• You can't extend more than one class!
– the derived class can't have more than one base
class.
• You can do multiple inheritance with interface
inheritance.
![Page 18: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/18.jpg)
Java Programming: OOP 78
Inheritance Example cont.
• Employee: name, email, phone
– FulltimeEmployee: also has salary, office, benefits, …
• Manager: CompanyCar, can change salaries, rates contracts, offices, etc.
– Contractor: HourlyRate, ContractDuration, …
• A manager a special kind of FullTimeEmployee,
which is a special kind of Employee.
• The relationship modeled by inheritance is often
referred to as a “is a” relationship
![Page 19: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/19.jpg)
Inheritance Example
79
Employee
------------------
Name
Phone
FulltimeEmployee
------------------------
Salary
Office
Manager
Manager
------------------
CompanyCar
Phone
Contractor
------------------
HourlyRate,
ContractDuration
![Page 20: IT203 OOP Introduction to OOP SJP ART - …...2015/03/03 · Java Programming: OOP 62 Inheritance • On the surface, inheritance is a code re-use issue. – we can extend code that](https://reader034.vdocuments.net/reader034/viewer/2022042621/5f4d568b3217f23b8f483c6a/html5/thumbnails/20.jpg)
Inheritance Example
80
Employee
------------------
Name
Phone
FulltimeEmployee
------------------------
Salary
Office
Manager
Manager
------------------
CompanyCar
Phone
Contractor
------------------
HourlyRate,
ContractDuration
Is a
Is a Is a