Bài giảng Phát triển phần mềm phân tán - Bài 7: Inheritance - Vũ Thị Hương Giang
22-Oct-16
Objectives
LESSON VII.
Inheritance
•
Understand and master some Java techniques for
realizing the inheritance
Vu Thi Huong Giang
SoICT (School of Information and
Communication Technology)
HUST (Hanoi University of Science
and Technology)
Content
•
•
•
•
Method overriding
I. METHOD OVERRIDING
Single inheritance and multiple inheritance
Abstract class and abstract method
Interface and implementation
1. Concept
2. Final modifier and overriding
3. Object class
1. Concept
Method overriding: example
// Account class
class Account
public class ChargeAccount extends Account{
// Additional member variables
{
•
•
The sub class redefine a method that is inherited
from a super class
The redefined method must have the same
signature as the parent's method, but can have a
different body
// Member variables
protected String owner;
protected long balance;
private int overdraft;
private int overdraft_limit;
//…
// access to the super class' member
//…
public void display()
System.out.print("Owner:"
owner);
{
public void display() {
+
System.out.println("\t\t Borrowing
amount limit:"+ overdraft_limit);
System.out.println("\t\t Borrowing
•
The type of the object executing the method
determines which version of the method is called.
System.out.println("\t Balance:"
+
balance);
amount:" + overdraft);
}
}
}
}
1
22-Oct-16
Method overriding: example
2. Final modifier and overriding
public class ChargeAccountClassUsage
public static void main(String[] args)
// creat super class object
Account account new Account();
{
•
•
A class may be declared as final
{
–
that class may not be extended
a
=
A method in a class may be declared as final
// create and initiate
ChargeAccount chargeacc
a sub class object
=
–
–
–
that method may not be overridden
guarantees behavior in all descendants
can speed up a program by allowing static binding
(binding or determination at compile time what code will
actually be executed)
new ChargeAccount();
chargeacc.setData("Giang", 1000000);
chargeacc.setOverdraftLimit(1000);
chargeacc.loan(2000);
// (1) call overridden method
chargeacc.display();
// (2) can not call method from its super class, once it is overridden
// why
? The object executing the method is of type ChargeAccount
((Account) chargeacc).display();
Account account1;
•
All static methods and private methods are
implicitly final, as are all methods of a final class.
account1
account1.display(); // (3) using display() of ChargeAccount
account1 account;
account1.display(); // (4) using display() of Account
= chargeacc;
=
}
}
Final modifier in inheritance: example
3. Object Class
// Account class
final class Account
public class ChargeAccount extends Account{
// Additional member variables
private int overdraft;
{
•
Object class is a super-class of all Java classes:
// Member variables
–
Object is the root of the Java inheritance hierarchy.
A variable of the Object type may refer to objects of any
class.
As arrays are implemented as objects, it may also refer
to any array.
protected String owner;
protected long balance;
private int overdraft_limit;
//…
// access to the super class' member
–
// Declaring
a class final
// implicitly declares
// all its methods final
–
public void display() {
public void display()
{
System.out.println("\t\t Borrowing
amount limit:"+ overdraft_limit);
System.out.println("\t\t Borrowing
System.out.print("Owner:"
owner);
+
System.out.println("\t Balance:"
amount:" + overdraft);
+
balance);
}
}
}
}
// It is illegal to declare a class
// as both abstract and final.
final abstract class Account
{
...
}
Overriding the Object class’ methods
Methods that can be
overridden
Methods that can not be
overridden
II. SINGLE INHERITANCE AND
MULTIPLE INHERITANCE
•
•
•
•
•
Object clone()
void finalize()
int hashCode()
String toString()
•
•
•
•
•
void notify()
void notifyAll()
Class getClass()
void wait()
boolean
void wait(long
milliseconds)
equals(Object
object)
•
void wait(long
milliseconds, int
nanoseconds)
1. Inheritance chain
2. Single and multi-level inheritance
3. Multiple inheritance
2
22-Oct-16
Inheritance chain
Single and multi-level inheritance
Object
•
Single inheritance:
•
Multi-level inheritance:
•
•
The path of inheritance
over the classes
Each class have only one
parent or super class
–
there are only one direct super
class from which a subclass
explicitly inherits.
–
A subclass inherits from any
class above its direct super
class in the class hierarchy
Shape
….
•
Example :
•
Example:
–
Triangle and 2DShape
–
Triangle and Object
2D Shape
Triangle
3D Shape
–
Shape and Object
–
Triangle and Shape
Object
Circle
Quadrilateral
Sphere
Polyhedron
Shape
….
2D Shape
Triangle
3D Shape
Circle
Quadrilateral
Sphere
Polyhedron
Multiple inheritance
•
A sub class in the hierarchy inherits from more
than one super classes in more than one
inheritance path.
III. ABSTRACT CLASSES AND
ABSTRACT METHODS
1. Abstract class
2. Abstract method
3. Example
1. Abstract class
2. Abstract method
•
An abstract class provides an outline from which
other classes inherit attributes and operations
•
Abstract methods are methods that do not have
implementation (body)
–
Provide implementation for some of methods that it
declares
public abstract
–
Subclasses that extend it must complete the class
definition
return-type method-signature;
To become concrete class, a sub class of an
abstract class must implement all abstract
methods of super abstract classes in the
inheritance chain.
Otherwise, this sub class will become an abstract
class and can not be instantiated.
Can not create instances of abstract classes
•
Syntax
public abstract class ClassName{
// definition of concrete methods
// declaration of abstract methods
}
18
3
22-Oct-16
3. Example
Example
// define the common attributes and
// operations for all kinds of shapes;
// define the operations reserved for
// all 2d shapes
public class Point extends
TwoDimensionShape{
private int x; //x-coordinate
private int y; //y-coordinate
public class Triangle extends
TwoDimensionShape{
private Point p1, p2, p3;
public abstract class TwoDimensionShape
extends Shape{
// This method will be implemented by
concrete class
public abstract class Shape
{
a
String name;
// concrete methods provided by
this class
public String getName()
{
public abstract double calculateArea();
public abstract double
calculatePerimeter();
return name;
}
// implementation of abstract
methods
public void display(){
System.out.print("Point");
System.out.print
}
/* This method is used to display
information about concrete kind
public void display(){
System.out.println
("Triangle");
p1.display();
p2.display();
p3.display();
System.out.println();
}
a
public class Triangle extends
TwoDimensionShape{
of shapes. It will be implemented
differently by concrete sub classes
*/
("("
+ x + "," + y + ")");
...
}
/*
all of abstract methods of Shape
and TwoDimensionShape must be
public abstract void display();
}
}
implemented here in
way. */
a specific
}
…
}
Example: abstract class reference
public abstract class Shape
String name;
{
public class ShapeUsage
...
{
IV. INTERFACE AND
IMPLEMENTATION
public String getName()
return name;
{
// illegal to create objects of
// the abstract class
}
Shape sh
=
new Shape();
variable with
public abstract void display();
// legal to create
a
}
// the abstract class type
TwoDimensionShape tsh;
public abstract class TwoDimensionShape
extends Shape{
public abstract double
calculateArea();
public abstract double
calculatePerimeter();
}
// legal to assign reference to any
// object of
Triangle tri
a
=
concrete sub class
new Triangle();
tsh
= tri;
double area
=
tsh. calculateArea();
}
public class Triangle
extends TwoDimensionShape{
...
1. Interface
}
1. Interface
Interface Variables declaration
•
Syntax
•
An interface defines a standard and public way of
specifying the behavior of classes
public static final type-name var-name
=
constant-expr;
•
•
This is a technique to import shared constants into multiple classes:
–
Classes that implement an interface must respect the
–
–
declare an interface with variables initialized to the desired values
include that interface in a class through implementation
methods’ return type and the signature as declared.
Variables declared in an interface must be constants
–
All declared method must be implemented
–
Every variable declaration in the body of an interface is implicitly public, static,
and final
•
Syntax
•
•
If the interface does not declare any method, the class does not
implement anything except importing the variables as constants.
Example :
public interface TwoDimensionShape
int DIMENSION 2;
[access-modifier] interface interface-name {
// variables (constant data)
{
// implicitly abstract and public methods
=
}
}
4
22-Oct-16
Interface methods declaration
Interface Inheritance
public interface Shape
{
•
•
One interface may inherit
another interface.
The method and the constant
that are defined in the super
interface are inherited to the
sub-interface.
The inheritance syntax is the
same for classes and
interfaces.
When a class implements an
interface that inherits another
interface, it must provide
implementations for all
•
•
[access-modifier] return-type method-name
(parameters list);
public String getName();
public void display();
Methods declared in an interface are implicitly
abstract an public
}
public interface TwoDimensionShape
extends Shape{
–
public: all others can be accessed
•
•
–
abstract: no implementation
public double calculateArea();
An interface can be considered as an abstract
class which contains only abstract methods.
public double
calculatePerimeter();
}
methods defined within the
interface inheritance chain.
Example
Why interface?
public interface Shape
{
public class Triangle implements
TwoDimensionShape{
•
•
To model multiple inheritance
public String getName();
public void display();
To reveal an object's programming interface
(functionality of the object) without revealing its
implementation
To have unrelated classes implement similar
methods (behaviors)
// declare variable here
private String name;
}
/* all of methods of Shape
and TwoDimensionShape
public interface
TwoDimensionShape extends
Shape{
•
must be
public double
calculateArea();
implemented here
in
a specific way. */
public double
calculatePerimeter();
…
}
}
28
Abstract class vs. interface
2. Interface implementation
•
Mix of abstract and
concrete methods
•
All methods are abstract
methods
•
Syntax:
[modifier] class class_name
[extends super-class-name]
implements comma-separated-list-of-interfaces
…
–
Abstract methods must
be declared explicitly
using abstract keyword
–
a subclass is required to
implement them
{
•
•
Contain attributes that
•
•
Can only define
constants
are inherent to a class
//
//
…
overridden methods
its own methods
Have one direct
Interfaces have no
inherited relationship
with its super class
direct inherited
relationship with any
particular class, they are
defined independently
}
•
A concrete class can only extend one super class, but it can
implement multiple interfaces
–
Interfaces themselves
have inheritance
relationship among
themselves
–
It is required to implement all abstract methods of all interfaces
30
5
22-Oct-16
Example: Using interface as a type
Quiz
public interface Shape
{
public class TriangleUsage
...
{
public String getName();
public void display();
// legal to create
a variable with
}
// the interface type
TwoDimensionShape tsh;
public interface TwoDimensionShape
extends Shape{
public double calculateArea();
// legal to refer to any object of any
// class implementing this interface
public double calculatePerimeter();
}
Shape tri
=
new Triangle();
new Triangle();
TwoDimensionShape tsh1
=
public class Triangle implements
TwoDimensionShape{
// call any method in the interface
// using the interface type variable
tri.display();
…
}
double area
= tsh1.calculateArea();
}
Review
Review
•
Method overriding:
•
Abstract class and abstract method
–
The sub class redefine a method that is inherited from a
super class.
–
Abstract class: outline from which other classes inherit
attributes and operations.
–
Abstract method: no implementation
•
Single inheritance and multiple inheritance
•
Interface and implementation
–
–
Single inheritance + multi-level inheritance: create a
new class as an extension of another class using extends
keyword
–
Interface: what a class must do
–
Interface implementation: complete set of methods
defined by this interface
•
Class could be either concrete or abstract
Multiple inheritance: create a new class to implement
the methods that are defined as part of an interface
using implements keyword
•
Class could implement more than one interface
6
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 7: 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:
bai_giang_phat_trien_phan_mem_phan_tan_bai_7_inheritance_vu.pdf

