Bài giảng Phát triển phần mềm phân tán - Bài 6: Aggregation and inheritance - Vũ Thị Hương Giang

21-Oct-16  
Objectives  
LESSON VI.  
Aggregation and Inheritance  
•
Understand the aggregation and inheritance  
relationships among classes and objects  
Vu Thi Huong Giang  
SoICT (School of Information and  
Communication Technology)  
HUST (Hanoi University of Science  
and Technology)  
Content  
•
Aggregation  
I. AGGREGATION  
–
Principles  
–
Order of initialization  
•
Inheritance  
–
–
–
Principles  
Inheritance hierarchy  
Sub class definition  
•
extends  
Order of initialization  
super  
–
1. Principles  
2. Order of initialization  
•
1. Principle  
Example: Point – an existing class  
public class Point  
{
•
Reusing through object:  
Create new functionality: taking  
existing classes and combining them  
into a new whole class  
MyNewClass  
private int x; // x-coordinate  
private int y; // y-coordinate  
public Point(){}  
–
Instance of  
existing class  
public Point(int x, int y)  
{
…
–
Create an interface comprising of  
public methods for this new class for  
interoperability with other code  
this.x  
this.y  
=
=
x;  
y;  
Instance of  
existing class  
}
•
Relation  
Existing class is a part of new whole  
class  
public void setX(int x){ this.x  
public int getX(){ return x;  
public void setY(int y){ this.y  
=
=
x;  
y;  
}
}
(Interface)  
public method  
…
–
}
–
New whole class has an existing class  
public int getY(){ return y;  
public void displayPoint(){  
}
Reuse attributes and operations of  
existing classes through the instances  
of these classes.  
public method  
System.out.print("("  
+ x + "," + y + ")");  
Etc.  
}
}
6
1
21-Oct-16  
Triangle – whole class  
Triangle - usage  
public class Triangle  
{
public class TriangleUsage  
{
private Point d1, d2, d3;  
public Triangle(Point p1, Point p2, Point p3){  
public static void main(String[] args)  
{
Point d1  
Point d2  
Point d3  
=
=
=
new Point(2,3);  
new Point(4,6);  
new Point (5,1);  
d1  
=
p1; d2  
= p2; d3 = p3;  
}
public Triangle(){  
d1  
d2  
d3  
=
=
=
new Point();  
new Point(0,1);  
new Point (1,1);  
Triangle triangle1  
Triangle triangle2  
=
=
new Triangle(d1, d2, d3);  
new Triangle();  
triangle1.displayTriangle();  
triangle2.displayTriangle();  
}
public void displayTriangle(){  
d1.displayPoint();  
d2.displayPoint();  
d3.displayPoint();  
System.out.println();  
}
}
}
1
3
}
Triangle  
Point  
7
8
2. Order of initialization  
•
Initialize all instances of reused  
existing classes  
MyNewClass  
INHERITANCE  
Instance of  
existing class  
–
Call their constructors  
…
•
Initialize an instance of the  
whole class  
Instance of  
existing class  
–
Call its constructor  
(Interface)  
public method  
…
public method  
1. Principles  
2. Subclass definition  
Etc.  
1. Principles  
Example  
•
Reusing through class: create a new class by extending the  
functionality of an existing class  
Super class  
Media  
Defining basic operations on  
media products  
–
The existing class is called the parent class, or super class, or base  
class  
All the behaviors of super class (public  
attributes, operations, relationships) are  
inherited by the sub-class  
–
The new class is called the child class, or subclass, or derived class  
•
Relation: New class is a kind of existing class  
–
A subclass inherits the operations, attributes and hierarchical  
relationships of its super class  
•
If a method is defined in a super class, all of its sub class inherit  
automatically this method  
Sub class Book  
•
If a set of member variables are defined in a super class, all of its sub  
classes inherit the same set of member variables  
Basic operations on media products  
+
•
To provide new functionality to a subclass, we can  
Define new methods and variables for this subclass only  
Super class  
Media  
Specific behaviors on books  
–
–
Override (execute instead of) methods of the super class in this  
subclass  
11  
12  
2
21-Oct-16  
2. Class hierarchy  
Example  
•
The class hierarchy defines the inheritance  
relationship between classes.  
•
The Shape class  
directly extends the  
Object class  
The Circle class is  
indirectly inherits from  
the Object class  
Object  
–
–
The root of the class hierarchy is the class Object.  
•
Every class in Java directly or indirectly extends (inherits  
from) this class.  
•
Direct super class: the super class from which a  
subclass explicitly inherits.  
Shape  
….  
–
Java: a class can only have one direct super class (single  
inheritance)  
2D Shape  
3D Shape  
•
•
Indirect super class: any class above the direct  
super class in the class hierarchy  
The constructors and destructors are not  
inherited.  
Circle  
Triangle  
Quadrilateral  
Sphere  
Polyhedron  
Visibility of inherited members  
3. Subclass definition  
•
•
A public member is accessed from any class  
A super classes protected members can be accessed by members of  
its subclasses and by members of classes in the same package.  
A private member is only accessible from inside the class in which it  
was declared.  
[access-modifier] class Subclass-name  
extends Superclass-name{  
//  
//  
//  
New added variables  
•
New methods  
Overridden methods  
Public  
member  
Protected  
member  
Private  
member  
Default  
member  
}
Inside class  
Yes  
Yes  
Yes  
Yes  
Yes  
No  
Yes  
Yes  
subclasses inside  
package  
subclasses outside  
package  
Yes  
Yes  
Yes  
No  
No  
No  
No  
No  
classes with  
non-inheritance  
relationship  
outside package  
16  
Example  
Instance of subclass  
// Media class (super class)  
// Book class (sub-class)  
public class Book extends Media  
protected String author[];  
protected int pages;  
Super class  
(Media)  
•
An instance of a  
subclass comprises  
of:  
public class Media  
{
{
protected String title;  
protected String category;  
protected float cost;  
public Book()  
{
–
member variables  
and methods that  
were defined by  
the super class  
member variables  
and methods that  
were added by the  
subclass  
...  
}
public void displayInstanceInfo()  
{
// new methods  
...  
}
public void setNumberofPages(int pages){  
title  
category  
cost  
this.pages  
= pages;  
...  
}
}
// overridden method  
–
public void displayInstanceInfo()  
{
//....  
author  
pages  
}
}
// Usage  
public class BookClassUsage  
public static void main(String[] args)  
Book obj new Book();  
obj.displayInstanceInfo();  
{
{
=
}
}
Sub class  
(Book)  
17  
18  
3
21-Oct-16  
Example: ChargeAccount - subclass  
Example: Account – super class  
public class ChargeAccount extends  
Account{  
public void loan(int overdraft)  
int current_overdraft  
this.overdraft overdraft;  
{
class Account  
{
=
// Member variables  
// Additional member variables  
private int overdraft;  
+
protected String owner; // Account name  
protected long balance; // Balance  
// value setting Methods  
if (current_overdraft <=  
overdraft_limit)  
// borrowing amount  
this.overdraft += overdraft;  
private int overdraft_limit;  
/* limit to the amount withdrawn  
else System.out.println("The  
limit to the amount  
withdrawn is exceeded !!!  
");  
public void setData(String name, long init_balance)  
{
*
*
from an account even though  
there is zero balance */  
owner  
=
name;  
init_balance;  
balance  
=
}
// Additional methods  
// overridden method  
}
public void setOverdraftLimit(int  
public void display()  
{
overdraft_limit)  
{
public void display()  
System.out.print("Owner:"  
System.out.println("\t Balance:"  
{
System.out.println("\t\t  
Borrowing amount limit:"+  
overdraft_limit);  
this.overdraft_limit  
overdraft_limit;  
=
+
owner);  
+
balance);  
this.overdraft  
= 0;  
System.out.println("\t\t  
}
}
Borrowing amount:"  
overdraft);  
+
}
}
}
Example: ChargeAccountClassUsage  
4. Initialization order  
public class ChargeAccountClassUsage  
public static void main(String[] args)  
// creat super class object  
Account account new Account();  
// create sub class object  
ChargeAccount chargeacc new ChargeAccount();  
{
•
•
Objects are constructed top-down under inheritance.  
Super class is initialized before its subclass  
{
a
=
–
first call super-class constructors  
a
–
then call sub-class constructors  
=
// sub class object calls methods from its super class: ok  
chargeacc.setData("Giang", 1000000);  
•
•
The constructors of the direct super class are always  
called  
chargeacc.setOverdraftLimit(1000);  
chargeacc.loan(2000);  
–
Explicit call: call the super class constructor from the sub  
class constructor code  
// super class object calls methods from its own class: ok  
account.setData("Tuan", 2000000);  
–
Implicit call: call the parameter-less or default constructor  
of the super class (if present); no call is written in the code  
// super class object calls methods from its sub class: no  
account.setOverdraftLimit(1000);  
// can not call method from its super class, once it is overridden  
chargeacc.display();  
Each super class’ constructor manipulates the  
instance variables that the subclass will inherit  
((Account) chargeacc).display();  
}
}
22  
Implicitly call of super class  
constructor: Example 1  
Implicitly call of super class  
constructor: Example 1  
public class Triangle  
{
public class IsoscelesTriangles extends Triangle{  
public IsoscelesTriangles()  
// automatic call Triangle()  
{
private Point d1, d2, d3;  
public Triangle  
System.out.println("IsoscelesTriangle constructor");  
(Point p1, Point p2, Point p3){  
}
d1  
= p1; d2 = p2; d3 = p3;  
}
}
public Triangle(){  
System.out.println("Triangle constructor");  
public class IsoscelesTriangleClassUsage  
{
d1  
d2  
d3  
=
=
=
new Point();  
public static void main(String[] args)  
{
new Point(0,1);  
new Point (1,1);  
IsoscelesTriangles obj new IsoscelesTriangles();  
=
}
}
}
public void displayTriangle(){  
d1.displayPoint();  
d2.displayPoint();  
d3.displayPoint();  
System.out.println();  
}
}
23  
4
21-Oct-16  
Implicitly call of super class  
constructor: Example 2  
Implicitly call of super class  
constructor: Example 3  
public class IsoscelesTriangles extends  
Triangle{  
public class IsoscelesTriangles extends  
Triangle{  
public IsoscelesTriangles() {  
public class Triangle  
{
public class Triangle {  
private Point d1, d2, d3;  
public void displayTriangle(){  
d1.displayPoint();  
d2.displayPoint();  
d3.displayPoint();  
System.out.println();  
}
private Point d1, d2, d3;  
public Triangle  
public IsoscelesTriangles()  
{
// automatic call Triangle();  
System.out.println  
("IsoscelesTriangle constructor");  
// automatic call Triangle();  
System.out.println  
("IsoscelesTriangle constructor");  
(Point p1, Point p2, Point p3){  
System.out.println  
}
}
("Triangle constructor");  
}
}
d1  
= p1; d2 = p2; d3 = p3;  
}
public class IsoscelesTriangleClassUsage  
public static void main(String[] args)  
IsoscelesTriangles obj  
new IsoscelesTriangles();  
{
public class IsoscelesTriangleClassUsage  
public static void main(String[] args)  
IsoscelesTriangles obj  
new IsoscelesTriangles();  
{
}
public void displayTriangle(){  
d1.displayPoint();  
d2.displayPoint();  
d3.displayPoint();  
System.out.println();  
}
{
{
=
=
}
}
}
}
}
Failed, because class Triangle has user-  
defined constructor, but no constructor  
Triangle() is found in the super class.  
25  
26  
5. super keyword  
Explicit call of super class constructor  
super  
(superclassMedia)  
•
•
This keyword is used to indicate  
the super class of the caller class  
Two usages of super:  
•
If the super class defines explicitly his  
constructors with parameter, the sub class must  
call them explicitly  
–
to call the super-class  
constructor  
title  
category  
cost  
–
Sub class constructor (either with or without  
parameters) calls the super class constructor in his first  
statement using super keyword.  
super(parameter list);  
to access super-class members  
super.variable;  
–
author  
pages  
•
•
The sub class could also call explicitly the  
constructor without parameter of its super class  
Depending on the passed argument when a  
constructor make a super call, the relevant  
constructor of super class is executed.  
super.method(parameter list);  
this  
(sub class Book)  
27  
Call super class’ constructor from a  
constructor without parameter  
Call super class’ constructor from a  
constructor without parameter  
public class RightAngleTriangle extends Triangle{  
public class Triangle  
{
public RightAngleTriangle()  
{
private Point d1, d2, d3;  
public Triangle(){  
// explicitly call the constructor with parameter  
// of super class  
d1  
d3  
=
=
new Point(); d2  
new Point (1,1);  
= new Point(0,1);  
super(new Point(0,0), new Point(1,0), new Point(0,2));  
System.out.println("RightAngleTriangle constructor");  
}
public Triangle(Point p1, Point p2, Point p3){  
}
System.out.println("Triangle constructor");  
}
}
d1  
= p1; d2 = p2; d3 = p3;  
public class RightAngleTriangleClassUsage  
{
}
public static void main(String[] args)  
{
public void displayTriangle(){  
d1.displayPoint();  
d2.displayPoint();  
d3.displayPoint();  
System.out.println();  
}
RightAngleTriangle obj new RightAngleTriangle();  
=
}
}
5
21-Oct-16  
Call super class’ constructor from a  
constructor with parameter  
Access super class’ members  
public class RightAngleTriangle extends Triangle{  
•
•
The super keyword allows accessing to the  
hidden variables or methods of the super-class  
When a sub-class declares the variables or  
methods with the same names and types as its  
super-class, the re-declared variables or methods  
hide those of the super-class.  
public RightAngleTriangle(Point p1, Point p2, Point p3)  
{
// explicitly call the constructor with parameter of super class  
super(p1, p2, p3);  
System.out.println("RightAngleTriangle constructor");  
}
}
public class RightAngleTriangleClassUsage  
public static void main(String[] args)  
RightAngleTriangle obj1 new  
RightAngleTriangle(new Point(0,0), new Point(1,0), new Point(0,2));  
{
{
=
}
}
Access super class’ method  
Access super class’ variable  
public class ChargeAccount extends Account{  
// Account class  
class Account  
public class ChargeAccount extends Account{  
// Account class  
class Account  
// Additional member variables  
private int overdraft;  
private int overdraft_limit;  
//…  
{
// Additional member variables  
private int overdraft;  
private int overdraft_limit;  
//…  
{
// Member variables  
protected String owner;  
protected long balance;  
// Member variables  
protected String owner;  
protected long balance;  
// access to the super class' member  
// access to the super class' member  
//…  
//…  
public void displayInfo()  
{
public void displayInfo()  
{
public void display()  
System.out.print("Owner:"  
System.out.println("\t Balance:"  
balance);  
{
// access super class method  
// super.displayInfo();  
public void display()  
System.out.print("Owner:"  
System.out.println("\t Balance:"  
balance);  
{
// access super class method;  
+
owner);  
+
owner);  
+
super.displayInfo();  
+
// access directly to super class’s variables  
// to do the same thing  
}
System.out.println("\t\t Borrowing  
amount limit:“+ overdraft_limit);  
System.out.println("\t\t Borrowing  
amount:“+ overdraft);  
}
}
//System.out.print("Owner:"  
//System.out.println("\t Balance:"  
+
super.owner);  
super.balance);  
}
+
}
System.out.println("\t\t Borrowing  
amount limit:“+ overdraft_limit);  
System.out.println("\t\t Borrowing  
amount:“+ overdraft);  
}
}
}
6. Final modifier  
Quiz 1: Aggregation  
•
The final modifier indicates a class that can not  
be sub-classed.  
• Implement the class Triangle in a different way  
–
Hint : using the array data type.  
–
Example  
public final class EquilateralTriangle extends  
Triangle{  
//  
}
•
•
If a method is declared with the final modifier, it  
can not be overridden  
If a variable is declared with the final modifier, it  
can not be changed after its initialization  
35  
6
21-Oct-16  
Quiz 1: Aggregation  
Another implementation of Triangle  
Quiz 2: Inheritance  
public class AnotherTriangle  
private Point[] Point  
{
•
Modify the Account and ChargeAccount class to  
visualize following cases  
=
new Point[3];  
public AnotherTriangle(Point p1, Point p2, Point p3){  
–
–
–
Encapsulation of the Account class  
Call the constructor of its own class  
Call the constructor of its super class  
Point[0]  
Point[1]  
Point[2]  
=
=
=
p1;  
p2;  
p3;  
}
–
Hint: using private, this and super keywords  
public void displayTriangle(){  
Point[0].displayPoint();  
Point[1].displayPoint();  
Point[2].displayPoint();  
System.out.println();  
}
1
3
Triangle  
Point  
}
37  
Quiz 2:  
Quiz 2: encapsulation  
call the constructor of its own class  
//Account class  
//Account class  
public class Account  
{
public class Account {  
// Member variables  
// Member variables  
private String owner; // Account name  
private long balance; // Balance  
private String owner; // Account name  
private long balance; // Balance  
// constructor with parameters  
// value setting methods  
public Account (String owner, long balance){  
public void setOwner(String name)  
this.owner name;  
}
{
=
this.owner  
=
owner;  
= balance;  
this.balance  
}
public void setBalance(long balance){  
this.balance balance;  
=
}
public Account() {  
// constructor call of own class  
// value getting methods  
public String getOwner(){  
return this.owner;  
}
public long getBalance(){  
return this.balance;  
}
this("My name", 1);  
}
public void display()  
System.out.print("Owner:"  
System.out.println("\t Balance:"  
{
+ owner);  
+
this.balance);  
}
}
}
Quiz 2: call the constructor of its super  
class  
Quiz 2: this and super  
public class ChargeAccount extends Account{  
// Additional member variables  
public class ChargeAccountUssage  
public static void main(String[] args)  
// Use the constructor without argument  
Account obj new Account();  
obj.display();  
{
{
private int overdraft; // borrowing amount  
private int overdraft_limit;  
=
//constructor of sub-class  
System.out.println();  
public ChargeAccount(String name, long balance, int overdraft_limit){  
// explicit call to super class;  
// For sub-class method, use superclass method  
super(name, balance);  
ChargeAccount obj2  
obj2.display();  
= new ChargeAccount("Vu Thi Huong Giang",10000,50000);  
this.overdraft  
= 0;  
this.overdraft_limit  
=
overdraft_limit;  
}
}
}
public void display()  
{
// access to the super class' method  
super.display();  
System.out.println("\t\t Borrowing amount limit:"+ overdraft_limit);  
System.out.println("\t\t Borrowing amount:"  
+ overdraft);  
}
}
7
21-Oct-16  
Quiz 3: Final modifier  
Quiz 3: final  
public class Circle  
{
•
The final keyword means that this variable can  
never be changed.  
public static final double PI  
public double x, y;  
= 3.14159;  
public double r;  
}
public class CircleClassUsage  
public static void main(String[] args)  
Circle.PI 4;  
{
{
=
}
Is this correct ?  
Review  
•
Aggregation:  
–
Create new functionality by taking other classes and  
combining them into a new class.  
–
Formulate an common interface to this new class  
•
Inheritance:  
–
Extend the functionality of a class by creating a  
subclass.  
–
Override super class members in the subclasses to  
provide new functionality  
8
pdf 8 trang Thùy Anh 19980 Free
Bạn đang xem tài liệu "Bài giảng Phát triển phần mềm phân tán - Bài 6: Aggregation and inheritance - Vũ Thị Hương Giang", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_phat_trien_phan_mem_phan_tan_bai_6_aggregation_and.pdf