Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Object-Oriented Programming

Lecture 5 – Inheritance, Composition and Aggregation

Ralph Tambala

MUST . CSIT

Lecture 5 1 / 14
Outline

1 Inheritance
Object Class
Syntax

2 Composition
Composition vs. Inheritance

3 Aggregation

Lecture 5 2 / 14
Inheritance

Inheritance (1)

Inheritance represents the IS A relationship which is also known as a


parent-child relationship.

For example,

Herbivore is a Animal, Hyena is a Carnivore, Man is a Omnivore, etc

Lecture 5 3 / 14
Inheritance

Inheritance (2)
Inheritance in Java is a process that allows one object to inherit all of the
properties and behaviors of a parent object. The idea behind inheritance is
that you can create new classes that are built upon existing classes. When
you inherit from an existing class, you can use or modify the parent class’s
methods and fields. You can also add new methods and fields to your
current class.

Consider the following scenario:


You have been tasked with creating a Student Academic Register
Information System (SARIS) for MUST. One factor to think about is the
kind of users that would use the system. For example, one requirement
may be that all users must register their full names, email addresses, and
user types. Different types of users may be able to perform different
operations on the system as a result. Consider the case where this scheme
has many common users (each with slightly different information and
operations). This is where inheritance comes to the rescue.
Lecture 5 4 / 14
Inheritance Object Class

Inheritance (3)
Object Class (1)

Class Object is the root of the class hierarchy. Every class has
Object as a superclass (parent). All objects, including arrays,
implement the methods of this class.
Every class in the Java system is a descendent (direct or indirect) of
the Object class. The Object class defines the basic state and
behavior that all objects must have, such as the ability to compare
oneself to another object, to convert to a string, to wait on a
condition variable, to notify other objects that a condition variable
has changed, and to return the object’s class.
We already looked at one important method that classes inherit from
the Object class (toString). The other methods include:
The equals Method – used to compare two objects for equality. This
method returns true if the objects are equal, false otherwise.
The getClass Method – Returns the Class class object of this object.
The Class class can further be used to get the metadata of this class.
Lecture 5 5 / 14
Inheritance Object Class

Inheritance (4)
Object Class (2)

For example, if we create a class User. In Java, class User


automatically becomes a child of Object class. In this case, User is
the derived class (the class that is derived from another class) and is
called a subclass. Also if we create a class Student, a subclass of
User class, then we can call User a superclass of Student.
As you saw in the previous lecture, all objects were able to use the
method toString which was inherited from Object class.
It should be noted that Java do not support multiple inheritance.
However, multiple inheritance can be achieved through interfaces.

Lecture 5 6 / 14
Inheritance Syntax

Inheritance (5)
Syntax

1 class SubclassName extends SuperclassName


2 {
3 // methods and fields
4 }

When you use the extends keyword, it means you are creating a new class
that is derived from an existing one.

Recall, in Java terminology a subclass is a child class whereas the


superclass is a parent class.

Lecture 5 7 / 14
Inheritance Syntax

Inheritance (6)
Example

Staff is a User.

1 class User { // superclass


2 String name ;
3 public void setName ( String name ) { this . name = name ; }
4 public String getName () { return this . name ; }
5 }
6 class Staff extends User { // subclass
7 float salary ;
8 public void displaySalary () {
9 System . out . println ( " Monthly salary is K10 . " ) ;
10 }
11 }
Lecture 5 8 / 14
Inheritance Syntax

Inheritance (7)
Example cont...

We did not define name attribute, setName method and getName


method explicitly in the Staff class. However, the extends keyword
means that we have inherited these. We can use them directly as if
there were defined inside Staff class.
1 // put this inside class Staff
2 public static void main ( String [] args ) { // main method
3 Staff s1 = new Staff () ;
4 s1 . setName ( " Dzina Langa " ) ; // from superclass
5 System . out . println ( s1 . getName () ) ; // from superclass
6 s1 . displaySalary () ;
7 }

In contrast, User class which is a superclass cannot access salary


attribute and displaySalary method of subclass Staff.

Lecture 5 9 / 14
Composition

Composition (1)

Composition is a design technique in which one of the class’s fields may be


an instance of another class.

Composition is a PART OF relationship. It is also called a strong HAS A


relationship.
1 public class Page {
2 // code
3 }
4 public class Book {
5 private Page page ;
6 public Book () {
7 this . page = new Page () ;
8 }
9 }
Pages are part of a Book.

Lecture 5 10 / 14
Composition Composition vs. Inheritance

Composition (2)
Composition vs. Inheritance

As we have seen, composition and inheritance both provide code


reusability by relating class. We can also get the functionality of
inheritance when you use composition. The table below provide their
differences:
Inheritance Composition
Composition is a HAS A rela-
Basic Inheritance is a IS A relationship
tionship
In Inheritance, a class lass can
extend only one interface, there- We can reuse code in multiple
Code Reusability
fore, you can reuse your code class
only in one class only
Inheritance provides its features Composition is easily achieved at
Scope
at compile time runtime
We can’t reuse code from the fi- It allows code reuse even from
Final
nal class final classes
It exposes both public and pro- It does not expose methods.
Methods tected method of the parent They interact using public inter-
class face.

Lecture 5 11 / 14
Aggregation

Aggregation (1)

Why is the difference between aggregation and composition so bleak


sometimes? The difference is that any composition is an aggregation and
not vice versa.

Both composition and aggregation are associations.


Composition – Strong HAS A relationship e.g. Every car has an
engine. We use line ending
Aggregation – Weak HAS A relationship. e.g. A car may have a
passenger or not.

Figure 1: Composition (left) and aggregation (right)

Lecture 5 12 / 14
Aggregation

Aggregation (2)
As seen in the code above, one can delete the car1 object and p1 will still
exists unlike in composition.
1 public class Passenger {
2 }
3
4 final class Car {
5 private Passenger passenger ;
6
7 void setPassenger ( Passenger passenger ) {
8 this . passenger = passenger ;
9 }
10
11 public static void main ( String [] args ) {
12 Car car1 = new Car () ; // exists independently
13 Passenger p1 = new Passenger () ;
14 car1 . setPassenger ( p1 ) ; // weak HAS A
15 }
16 }

Lecture 5 13 / 14
Aggregation

practice

Lecture 5 14 / 14

You might also like