07-Abstract Class and Interface
07-Abstract Class and Interface
Outline
1. Redefine/overriding
OBJECT-ORIENTED LANGUAGE AND THEORY
2. Abstract class
7. ABSTRACT CLASS AND INTERFACE
3. Single inheritance and multi-inheritance
4. Interface
1 2
3 4
3 4
1
5 6
5 6
7 8
7 8
2
9 10
class Shape {
protected String name;
class Square extends Shape {
Shape(String n) { name = n; } private int side;
public String getName() { return name; } Square(String n, int s) {
public float calculateArea() { return 0.0f; } super(n);
}
side = s;
class Circle extends Shape {
private int radius; }
Circle(String n, int r){ public float calculateArea() {
super(n); float area = (float) side * side;
radius = r; return area;
}
}
public float calculateArea() { }
float area = (float) (3.14 * radius * radius);
return area;
}
}
9 10
11 12
11 12
3
13 14
package abc;
public class Person { Overriding Rules
private String name;
private int age;
public String getDetail() {
• Overriding methods must have:
String s = name + "," + age; • An argument list that is the same as the overriden
return s; method in the parent class => signature
}
• The same return data types as the overriden method
private void pM(){}
} in the parent class
• Can not override:
import abc.Person;
public class Employee extends Person { • Constant (final) methods in the parent class
double salary; • Static methods in the parent class
public String getDetail() {
String s = super.getDetail() + "," + salary • Private methods in the parent class
return s;
}
}
13 14
15 16
15 16
4
17 18
17 18
20
19 20
5
22
21 22
23 24
23 24
6
25
25 26
27 28
7
29 30
Outline
• abstract class A {
abstract void a(); 1. Redefine/overriding
•} 2. Abstract class
• class B extend A {
•} 3. Single inheritance and multi-inheritance
4. Interface
29 30
31
A E F Bird
31 32
8
33
Outline Interface
Drawable
2. Abstract class
3. Single inheritance and multi-inheritance Circle Sphere
Rectangle Cube
4. Interface
Square
33 35
Interface Interface
• Interface: Corresponds to different implementations. • Interface does not implement any methods but defines the
• Defines the border: design structure in any class that uses it.
• What and How
• Declaration and Implementation. • An interface: 1 contract – in which software development
teams agree on how their products communicate to each
other, without knowing the details of product
implementation of other teams.
36 37
9
Example Interface OperateCar
• Class Bicycle – Class StoreKeeper: public interface OperateCar {
• StoreKeepers does not care about the characteristics what
// Constant declaration– if any
they keep, they care only the price and the id of products.
• Class AutonomousCar– GPS: // Method signature
• Car manufacturers produce cars with features: Start, Speed- int turn(Direction direction, // An enum with values RIGHT, LEFT
up, Stop, Turn left, Turn right,.. double radius, double startSpeed, double endSpeed);
• GPS: Location information, Traffic status – Making decisions int changeLanes(Direction direction, double startSpeed, double
for controlling car endSpeed);
int signalTurn(Direction direction, boolean signalOn);
- How does GPS control both car and space craft?
int getRadarFront(double distanceToCar, double speedOfCar);
int getRadarRear(double distanceToCar, double speedOfCar);
......
// Signatures of other methods
}
38 39
Shape Action 41
#x: int
#name: String
Class OperateBMW760i +getName():String
+calculateArea():float
#y: int
+draw(Graphics)
+moveTo(Graphics,int, int)
// Car Manufacturer +erase(Graphics)
}
Circle
// Các phương thức khác, trong suốt với các clients của -radius:float
interface +calculateArea():float
+draw(Graphics)
} +moveTo(Graphics,int,int)
+erase(Graphics)
40 41
10
42
interface TVInterface {
public void turnOn();
public void turnOff();
public void changeChannel(int i);
}
class PanasonicTV implements TVInterface{
public void turnOn() { …. }
}
42 43
44 45
44 45
11
46 47
import java.awt.Graphics;
Example abstract class Shape {
protected String name;
protected int x, y;
<<interface>>
Shape Actable Shape(String n, int x, int y) {
#name: String #x:int #y:int name = n; this.x = x; this.y = y;
+getName():String +draw(Graphics)
+calculateArea():float
+moveTo(Graphics,int, int) }
+erase(Graphics)
public String getName() {
return name;
Circle }
-radius:float public abstract float calculateArea();
+calculateArea():float }
+draw(Graphics)
+moveTo(Graphics,int,int) interface Actable {
+erase(Graphics)
public void draw(Graphics g);
public void moveTo(Graphics g, int x1, int y1);
public void erase(Graphics g);
}
46 47
48 49
class Circle extends Shape implements Actable {
private int radius;
public Circle(String n, int x, int y, int r){
super(n, x, y); radius = r;
Abstract class vs. Interface
}
public float calculateArea() { • May or may not • Can contain only
float area = (float) (3.14 * radius * radius);
return area; contain abstract method signature
} methods, can contain • Can contain only
public void draw(Graphics g) {
System out println("Draw circle at ("
instance methods public functions
+ x + “," + y + ")");
• Can contain protected without implementation
g.drawOval(x-radius,y-radius,2*radius,2*radius);
} and static methods • Can contains only
public void moveTo(Graphics g, int x1, int y1){
erase(g); x = x1; y = y1; draw(g); • Can contain final and constant attributes
} non-final attributes • A class can inherite
public void erase(Graphics g) {
System out println(“Erase circle at (" • A class can inherit only multiple interfaces
+ x + “," + y + ")"); one abstract class
// paint the region with background color...
}
}
48 49
12
50
50 51
3/11/2010
52
}
abstract void display(); implements Shape3D {
Point3D center;
double radius;
interface Comparable /java.lang
class Circle extends Shape Sphere(Point3D center, double radius) {
implements Shape2D { this.center = center;
this.radius = radius; Result :
Point3D center, p; // p is an point on
circle } Circle
3.141592653589793
Circle(Point3D center, Point3D p) { public void display() { Sphere
System.out.println("Sphere");
this.center = center; 4.1887902047863905
}
this.p = p;
} public double getVolume() {
return 4 * Math.PI * radius * radius * radius / 3;
}
public void display() {
}
System.out.println("Circle");
} class Shapes {
52 53
13
Application Application
54 55
56 57
void draw();
56 57
14
59
public class MultiInheritance implements Interface1, Interface2 { public class MultiInheritance2 extends Parent implements Interface3 {
@Override public static void main(String[] args) {
public void doSomething() { MultiInheritance2 m = new MultiInheritance2();
Interface1.super.doSomething(); m.doSomething(); // Execute in Parent
} }
} }
58 59
60 61
60 61
15
62 63
62 63
64
Static method
interface Interface3 {
default void doSomething() {
System.out.println("Execute in Interface3");
}
public static void test() {
System.out.println("test");
}
}
abstract class Parent {
public void doSomething() {
System.out.println("Execute in Parent");
}
}
public class MultiInheritance2 extends Parent implements Interface3 {
public static void main(String[] args) {
MultiInheritance2 m = new MultiInheritance2();
MultiInheritance2.test(); // ERROR!!!
}
}
64
16