You are on page 1of 24

http://fskik.upsi.edu.

my

Object Oriented Programming


(MTS 3033)

CHAPTER 15:
INHERITANCE
http://fskik.upsi.edu.my

Introduction to Inheritance
• Inheritance is one of the main techniques of object-
oriented programming (OOP)
• Using this technique, a very general form of a class is
first defined and compiled, and then more specialized
versions of the class are defined by adding instance
variables and methods
– The specialized classes are said to inherit the
methods and
instance variables of the general class

2
http://fskik.upsi.edu.my

The “is a” Relationship


• The relationship between a superclass and an inherited
class is called an “is a” relationship.
– A student “is a” person.
– A cow “is a” animal.
– A bicycle “is a” vehicle.
• A specialized object has:
– all of the characteristics of the general object, plus
– additional characteristics that make it special.
• In object-oriented programming, inheritance is used
to create an “is a” relationship among classes.

3
http://fskik.upsi.edu.my

The “is a” Relationship


• We can extend the capabilities of a class.
• Inheritance involves a superclass and a subclass.
– The superclass is the general class and
– the subclass is the specialized class.
• The subclass is based on, or extended from, the
superclass.
– Superclasses are also called base classes, and
– subclasses are also called derived classes.
• The relationship of classes can be thought of as
parent classes and child classes.

4
http://fskik.upsi.edu.my

Inherited Members
• A derived class automatically has all the instance variables,
all the static variables, and all the public methods of the
base class
– Members from the base class are said to be inherited
• Definitions for the inherited variables and methods do
not appear in the derived class
– The code is reused without having to explicitly copy it,
unless the creator of the derived class redefines one or
more of the base class methods

5
http://fskik.upsi.edu.my

Protected Modifier/Accessor
• Protected modifier is used to control access to the members
of a class.
• A variable or method declared with protected visibility may
be accessed by any class in the same package. In other
words a derived class can reference it.
• A protected visibility allows a class to retain some
encapsulation properties. However, the encapsulation is
not as tight as private.

6
http://fskik.upsi.edu.my

Example Derived/Subclass Classes


• An arrow with an open
Book
arrowhead is used to # pages: int
show
+ getPages() : int
inheritance. + setPages(int) : void
• The symbol # is used to
represent protected
access.
Dictionary
- definition: int
+ computeRatio() : double
+ setDefinitions(int) : void
+ getDefinitions() : int

7
http://fskik.upsi.edu.my

Example Derived Classes


• When a derived class is defined, it is said to inherit
the instance variables and methods of the base class
that it extends
– Class Book defines the instance variables pages in its
class definition
– Class Dictionary also has these instance variables, but they
are not specified in its class definition.
– Class Dictionary has additional instance variable
definitions that are specified in its class definition.

8
http://fskik.upsi.edu.my

Example Derived Classes


• Just as it inherits the instance variables of the class Book,
the class Dictionary inherits all of its methods as well
– The class Dictionary inherits the methods getPages,
and
setPages from the class Book
– Any object of the class Dictionary can invoke one of
these methods, just like any other method.

9
Programming Example

10
http://fskik.upsi.edu.my

Program description
• Write a complete Java program that
contains classes as shown below:
– The Worker class serves as the parent of all
classes and contains information that applies to
all workers. Each worker has a name of type
String. The Worker class contains a toString
method to return the name of the worker.

11
http://fskik.upsi.edu.my

Program description
• – The HourlyWorker class represents a worker
that earns an income based on hourly worked.
The HourlyWorker class contains of an instance
variable i.e. hour of type integer that represents
the number of hours worked. It consists of an
earning method that returns the result of hour
*
• 5.00 (rate per hour). It also consists of a
toString method to return the worker
information and his/her income.
12
http://fskik.upsi.edu.my

Program description
– The MonthlyWorker class represents a worker that earns an
income in a monthly basis. It contains an instance variable
i.e. salary of type double that represents the monthly salary.
It consists of an earning method that returns the salary. It
also contains a toString method to return the worker
information and his/her monthly income.
– The TestWorker class represents the application program
that instantiates HourlyWorker and MonthlyWorker
objects, and then invoke their toString() method.

13
14
http://fskik.upsi.edu.my

TestWorker.java
1 class Worker //Superclass
2 {
3 Private String name;
4
5 Public Worker(String
6 n) //Constructor
7 name = n;
8}
9
10 public String toString(){
11 return "Name :"+name;
12 }
13 }
14
15 class HourlyWorker extends Worker {
16 private int hour;
17
18 public HourlyWorker(String n, int h) {
19 super(n);
hour = h; 1
} 5
http://fskik.upsi.edu.my

TestWorker.java
20 public double earning() {
21 return hour * 5.00;
22 }
23 public String toString(){
24 return super.toString()+"\nHours worked:“ + hour+
25 "\nEarning:"+earning();
26 }
27 }
28
29 class MonthlyWorker extends Worker {
30 private double salary;
31
32 public MonthlyWorker(String n,
33 double s) {
34 super(n); //ambil dari
35 superclass Worker
36 salary = s;
37 }
38
39 public double earning() {
return salary; 1
6
}
http://fskik.upsi.edu.my

TestWorker.java
40 public String toString(){
41 return super.toString() + "\nEarning:"+ earning();
42 }
43 }
44
45 public class TestWorker {
46 public static void main(String[] args)
47 { HourlyWorker h1 = new
48 HourlyWorker("Ali",6); //object reference
49 System.out.println (h1.toString());
50
51 MonthlyWorker h2 = new
52 MonthlyWorker("Abu",6000);
53 System.out.println (h2.toString());
}
}

17
http://fskik.upsi.edu.my

Types of Inheritance
• Single Inheritance
• Multilevel Inheritance
• Hierarchical Inheritance
• Multiple Inheritance
• Hybrid Inheritance
http://fskik.upsi.edu.my

Single Inheritance
• A single subclass extends from a single
superclass
http://fskik.upsi.edu.my

Multilevel Inheritance
• A subclass extends from a superclass and the
same subclass acts as a superclass for another
subclass.
http://fskik.upsi.edu.my

Hierarchical Inheritance
• Multiple subclasses extends from a single
superclass.
http://fskik.upsi.edu.my

Multiple Inheritance
• A single subclass extends from multiple
superclasses.

Note: Java doesn’t support multiple inheritance. But by can achieve using interface.
http://fskik.upsi.edu.my

Hybrid Inheritance
• Combination of two or more type of
inheritance.
http://fskik.upsi.edu.my

Interface in Java
Characteristics of Interfaces:
1. Method in interface NO BODY
2. Implemented by the class
3. The class that implement interface MUST
implement all the method in the interface
4. Syntax is using keyword “interface”

You might also like