0% found this document useful (0 votes)
10 views11 pages

6. Aggregation and Inheritance

Uploaded by

Đỗ Hoàng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
10 views11 pages

6. Aggregation and Inheritance

Uploaded by

Đỗ Hoàng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

6.

Aggregation and Inheritance


1. Source code re-usability
Source code re-usability

Re-use already existing source code.

Structure programming: re-use func/sub-program

OOP: when modeling real world, there exist many obj types that have similar
or related attributes and behaviors.

How to use existing classes?

Copying existing classes -> Redundant and difficult to manage changes


Creating new classes that re-use of objects of existing classes ->
Aggregation (tổng hợp)
Creating new classes based on the extension of existing classes ->
Inheritance

Advantages

Reducing man-power, cost.


Improving software quality.
Improving modeling capacity of the real world.
Improving maintainability

2. Aggregation (tổng hợp)


Aggregation

The whole class contains obj of member classes


Is-a-part (là một phần) of the whole class
Reuse data and behavior of member classes via member objs

Representing aggregation in UML

Using Diamond at the head of whole class

Using multiplicity at two heads:

Positive integer: 1,2,3,4


Range: (0..1, 2..4)
* : any number
none : equal 1 by default

role name : if they dont have, by default is class name (uncapitalize the
first word)

Example in Java

class Point{
private int x, y;
public Point (){}
public Point (int x, int y){
this.x = x;
this.y = y;
}
public void setX(int x){ this.x = x;}
public int getX(){ return x;}
public void print(){
System.out.print("(" + x + ", "+ y + ") ");
}
}
class Quadrangle{
private Point[] corners = new Point[4];
public Quadrangle (Point p1, Point p2, Point p3, Point p4){
corners[0] = p1; corners[1] = p2;
corners[2] = p3; corners[3] = p4;
}
public Quadrangle(){
corners[0] = new Point();
corners[1] = new Point(0,1);
corners[2] = new Point(1,1);
corners[3] = new Point(1,0);
}
public void print(){
corners[0].print();
corners[1].print();
corners[2].print();
corners[3].print();
System.out.println();
}
}
public class Test {
public static void main(String arg[]){
Point p1 = new Point(2,3);
Point p2 = new Point(4,1);
Point p3 = new Point(5,1);
Point p4 = new Point(8,4);
Quadrangle q1 = new Quadrangle(p1,p2,p3,p4);
Quadrangle q2 = new Quadrangle();
q1.print();
q2.print();
}
}

Initialization order in aggregation


When an ob is created, the attributes of that obj must be initialized and
assigned corresponding values.

Member obj must be initialized first

Constructor methods of member classes must be called first.

3. Inheritance
A quadrangle has 4 points -> Aggregation
A square is an Quadrangle -> Inheritance

Inherit, Derive

Creating a new class by extending existing classes.


New class inherits members of existing classes and implement its own
new features.

Existing Class

Parent class, superclassm, base class

New Class

Child class, subclass, derived class

What is Inheritance?

On Modularization view: if B inherits A, all services of A will be available in B

On Type view: If B inherits A, anywhere requires representation of A, B can


replace A

=> Polymorphism

Child classes
Is a kind of parent
Reuse by inheriting data and behavior of parent
Can be customized in two ways (or both):
Extension: add more new attributes/behaviors
Redefinition (Method Overriding): modify the behavior inheriting
from parent class.

Aggregation and Inheritance


Both are OOP technique to reuse resource code.

Aggregation Inheritance
Reuse source code via Obj. Reuse source code via Class
Creating a reference to the obj of existing Creating new class by extending
classes in the new class existing classes.

is-a-part-of relation is-a-kind-of relation

Example

Inheritance: Car is a kind of Transportation


Aggregation: Car has 4 wheels

Representing Inheritance in UML


Using empty triangle at parent class

Inheritance hierarchy

Info

A hierachy structure, which represents inheritance relation between classes.

Direct inheritance: Sport Car <- Vehicle

Indirect inheritance: Car <- Vehicle

Note

Child classes have the same parent class called siblings


A child class inherits features from all of its ancestors
Class Object

Defined in the standard package java.lang

If a class is not defined as a child of another class, it is by default a


direct child of class Object .

Class Object is the root class in all hierachy trees

More about Object Class

Inheritance rules

What does a child class inherit?

Note

All attributes/methods that are declared as public and protected in


the parent class
All members with the default access modifier, if child and parent
classes are in the same package
Does not inherit private attributes/methods and
Construction/destruction methods

Example in Java
<SubClass> extends <SuperClass>

// if they are in different class


import <PackageName>.<ClassName>
}

Construction and Destruction of objs in Inheritance

Obj Construction

Parent class is initialized before its child classes.


Construction methods of a child class always call construction methods
of it parent class at the very first command.
Implicit call (khong ro rang)
Explicit call

Obj Destruction

Contrary to obj initialization.


Explicit constructor call of parent class

The first cmd in constructor of a child class can explicit call the constructor
of its parent class

super(list_of_params);

Caution
This is obliged if the parent class does not have any default constructor

Parent class already has a constructor with arguments


The constructor of child class must not have arguments

Implicit call of constructors


When initializing an obj, a serie of constructors will be called explicitly (via
super() or implicity call)

Constructor of the most basic class in the hierarchy tree will be called last,
but will finish first. The constructor of the derived class will finish at the last.

Implicit call of finalize()

Inverse compared to the call of constructors

You might also like