07-Abstract Class and Interface
07-Abstract Class and Interface
vn 2
Outline
1. Redefine/Overiding
OBJECT-ORIENTED LANGUAGE AND THEORY
2. Abstract class
7. ABSTRACT CLASS AND INTERFACE
3. Single inheritance and multi-inheritance
Nguyen Thi Thu Trang
[email protected]
4. Interface
3 4
1
5 6
class Shape {
1. Re-definition or Overriding (2) protected String name;
Shape(String n) { name = n; }
public String getName() { return name; }
• Overriding method will replace or add more details to the
public float calculateArea() { return 0.0f; }
overriden method in the parent class }
• Objects of child class will use the re-defined method class Circle extends Shape {
private int radius;
Circle(String n, int r){
super(n);
radius = r;
}
7 8
2
9 10
package abc;
this and super public class Person {
protected String name;
• this and super can use non-static protected int age;
methods/attributes and constructors public String getDetail() {
String s = name + "," + age;
• this: searching for methods/attributes in the
return s;
current class }
• super: searching for methods/attributes in the }
direct parent class
import abc.Person;
• Keyword super allows re-using the source-code public class Employee extends Person {
of a parent class in its child classes double salary;
public String getDetail() {
String s = super.getDetail() + "," + salary;
return s;
}
}
11 12
3
13 14
package university;
public class Faculty extends Person {
Person, Student và Faculty private int hireYear; Class Faculty
public Faculty( ) { super( ); hireYear = -1; }
public Faculty( String n, String id, int yr ) {
super(n, id);
hireYear = yr;
}
public Faculty( Faculty f ) {
this( f.getName( ), f.getIdNum( ), f.hireYear );
}
int getHireYear( ) { return hireYear; }
void setHireYear( int yr ) { hireYear = yr; }
public String toString( ) {
return super.toString( ) + " " + hireYear;
}
public boolean equals( Faculty f ) {
return super.equals( f ) && hireYear == f.hireYear;
}
}
4
Overriding Re-definition with final
• When a derived class wants to change a function inherited • Sometimes we want to restrict the re-definition because
from its parent class (super). of:
• Correctness: The re-definition of a method in a derived class
public class Person {
…
can lead the method to a wrong behavior
public String toString( ) { … } • Efficiency: The dynamic linking mechanism is not efficient in
} time as the static linking mechanism. If a method should not
public class Student extends Person {
Re-define the method of be re-defined in derrived classes, we shold use the keyword
…
the parent class final with the method
public String toString( ) { … }
}
Student bob = new Student("Bob Goodstudent","123-45-
6789",2004,4.0 ); public final String baseName () {
System.out.println( "Bob's info: " + bob.toString( ) ); return “Person”;}
}
Calling to the method of the child
class
5
Derived class Speedboat Class Book2
public class Speedboat extends Ship {
private String color = "red"; class Book2 {
public Speedboat(String name) { protected int pages;
super(name);
setSpeed(20); public Book2(int pages) {
} this.pages = pages;
public Speedboat(double x, double y, double speed, }
double direction, String name, String color) {
super(x, y, speed, direction, name); public void pageMessage() {
setColor(color); System.out.println("Number of pages: " +
} pages);
public void printLocation() { }
System.out.print(getColor().toUpperCase() + " "); }
super.printLocation();
}
...
6
Class Book3 Class: Dictionary3a
class Book3 { class Dictionary3a extends Book3 {
protected String title; private int definitions;
protected int pages;
public Dictionary3a(String title, int pages,
public Book3(String title, int pages) { int definitions) {
this.title = title; super (title, pages);
this.pages = pages; this.definitions = definitions;
} }
7
Class: Books Examples: Point, Circle, Cylinder
Kết quả:
C:\Examples>java Books
Title: Introduction to Java
Number of pages: 350
8
Problems in Inheritance
• Casting an object of a parent class to an
object of its derived class is called Instrument
upcasting
Wind
9
Upcast Down Cast
• Car c = new Car(); • When assigning an object of a more basic type to a
• ElectricCar ec = new ElectricCar (); derived type.
• c = ec; • Be careful in usage
• Do it explicitly
• Automatic upcast (implicit)
Car c = new ElectricCar(); // Up-casting not
• Types of objects do not be changed
explicitly
c.recharge(); // Error
// Down-casting explicitly
ElectricCar ec = (ElectricCar)c;
ec.recharge(); // ok
((ElectricCar)c).recharge();
10
41
43
11
45 46
47 48
12
Abstract Class Abstract Class
abstract class Point { abstract class ColoredPoint extends Point {
int color;
private int x, y;
public ColoredPoint(int x, int y, int color) {
public Point(int x, int y) { super(x, y); this.color = color; }
this.x = x; }
this.y = y;
} class SimpleColoredPoint extends ColoredPoint {
public SimpleColoredPoint(int x, int y, int color){
public void move(int dx, int dy) { super(x,y,color);
x += dx; y += dy; }
plot(); public void plot() {
} ...
// code to plot a SimplePoint
public abstract void plot();
}
} }
52
13
53
A E F Bird
D
Resolution of these problems is implementation-dependent.
55
14
Interface Interface
• Interface: Corresponds to different implementations.
2DShape 3DShape
• Defines the border:
Drawable • What How
• Declaration and Implementation.
Square
Interface Example
• Interface does not implement any methods but defines the • Class Bicycle – Class StoreKeeper:
design structure in any class that uses it. • StoreKeepers does not care about the characteristics what
they keep, they care only the price and the id of products.
• An interface: 1 contract – in which software development • Class AutonomousCar– GPS:
teams agree on how their products communicate to each
• Car manufacturers produce cars with features: Start, Speed-
other, without knowing the details of product
up, Stop, Turn left, Turn right,..
implementation of other teams.
• GPS: Location information, Traffic status – Making decisions
for controlling car
- How does GPS control both car and space craft?
15
Class OperateBMW760i
Interface OperateCar // Car Manufacturer
public interface OperateCar { public class OperateBMW760i implements OperateCar {
Shape Action 63 64
#x: int
#name: String
#y: int
4. Interface
+getName():String
+draw(Graphics)
+calculateArea():float
+moveTo(Graphics,int, int)
+erase(Graphics)
<<interface>>
Shape Actable
#name: String #x:int #y:int
+draw(Graphics)
+getName():String
+moveTo(Graphics,int, int)
+calculateArea():float +erase(Graphics)
Circle
-radius:float
+calculateArea():float
+draw(Graphics)
+moveTo(Graphics,int,int)
+erase(Graphics)
16
66
67 68
• Example: Circle
public interface Symmetrical {…} -radius:float
17
69 70
class Circle extends Shape implements Actable {
import java.awt.Graphics; private int radius;
abstract class Shape { public Circle(String n, int x, int y, int r){
protected String name; super(n, x, y); radius = r;
protected int x, y; }
public float calculateArea() {
Shape(String n, int x, int y) { float area = (float) (3.14 * radius * radius);
name = n; this.x = x; this.y = y; return area;
} }
public void draw(Graphics g) {
public String getName() {
System out println("Draw circle at ("
return name; + x + ," + y + ")");
} g.drawOval(x-radius,y-radius,2*radius,2*radius);
public abstract float calculateArea(); }
public void moveTo(Graphics g, int x1, int y1){
} erase(g); x = x1; y = y1; draw(g);
interface Actable { }
public void draw(Graphics g); public void erase(Graphics g) {
public void moveTo(Graphics g, int x1, int y1); System out println( Erase circle at ("
+ x + ," + y + ")");
public void erase(Graphics g);
// paint the region with background color...
} }
}
72
18
74
3/11/2010
}
abstract void display(); implements Shape3D {
Point3D center;
double radius;
interface Comparable /java.lang
class Circle extends Shape Sphere(Point3D center, double radius) { Result :
implements Shape2D { this.center = center;
this.radius = radius; Circle
Point3D center, p; // p is an point on
circle } 3.141592653589793
Sphere
Circle(Point3D center, Point3D p) { public void display() {
4.1887902047863905
System.out.println("Sphere");
this.center = center; }
this.p = p;
} public double getVolume() {
return 4 * Math.PI * radius * radius * radius / 3;
public void display() { }
}
System.out.println("Circle");
} class Shapes {
Application Application
19