7. Abstract Class and Interface
7. Abstract Class and Interface
Method Overloading
If the new method has the same name but different signature (number or
data types of method's arguments)
Overriding method will replace or add more details to the overriden method
in the parent class.
// current obj
this() // constructor from the other of the same class
this // refer to the current obj
// Parent class
super() // constructor of the parent class
super //refer to the parent class
Overriding Rules
An argument list that is the same as the overidden method in the parent
class (signature)
The same return data types as the overidden method in the parent class.
Ex: if a protected class is overrided, the overriding class can not have private
modifier but only protected or public
Example
class Parent{
public void doSth(){}
public int doSth2(){
return 0;
}
}
class Child extends Parent{
protected void doSth(){} // cannot override, since have a more
restricted access privileges (protected > public)
protected void doSth2(){} // cannot overrid, since have
different return type
}
Child pro
2. Abstract class
Abstract class
A class that can not create its objs, used to define Generic concepts, and
playing the role of a basic class for others detailed classes.
Note
Example
C++ multi-inheritance
Single Inheritance
Repeated inheritance
Check
Implementation-dependent
4. Interface
Corresponds to different implementations
Defines the border:
What and How
Declaration and Implementation
Interface
Note
Syntax
import java.awt.Graphics;
abstract class Shape {
protected String name;
protected int x, y;
Shape(String n, int x, int y) {
name = n; this.x = x; this.y = y;
}
public String getName() {
return name;
}
public abstract float calculateArea();
}
interface Actable {
public void draw(Graphics g);
public void moveTo(Graphics g, int x1, int y1);
public void erase(Graphics g);
}
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;
}
public float calculateArea() {
float area = (float) (3.14 * radius * radius);
return area;
}
public void draw(Graphics g) {
System out println("Draw circle at ("+ x + "," + y + ")");
g.drawOval(x-radius,y-radius,2*radius,2*radius);
}
public void moveTo(Graphics g, int x1, int y1){
erase(g); x = x1; y = y1; draw(g);
}
public void erase(Graphics g) {
System out println("Erase circle at ("+ x + "," + y + ")");
// paint the region with background color...
}
}
// InterfaceExample.java
interface WattleBottleInterface{
// only accept static and final attributes
String color = "Blue";
// methods have to be abstract.
void fillUp();
void pourOut();
}
public class InterfaceExample implements WaterBottleInterface{
public static void main(String[] args){
System.out.println(color);
InterfaceExample ex = new InterfaceExample();
ex.fillUP();
}
// all abstract methods of the interface have to be implemented in concrete
classs
@Override
public void fillUp(){
... // blocks of code
}
@Override
public void pourOut(){
...//blocks of code
}