0% found this document useful (0 votes)
12 views

7. Abstract Class and Interface

Uploaded by

Đỗ Hoàng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

7. Abstract Class and Interface

Uploaded by

Đỗ Hoàng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

7.

Abstract Class and Interface


1. Redefine/Overriding
A child class can define a method with the same name of a method in its
parent class:

Method Overloading

If the new method has the same name but different signature (number or
data types of method's arguments)

Method Overriding (Re-definition)

If the new method has the same name and signature

Overriding method will replace or add more details to the overriden method
in the parent class.

Objs of child class will use the re-defined method.

this and super

Can use non-static methods/attributes and constructors


Example

// 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

Overriding methods must have:

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.

Can not override:

Constant (final) methods in the parent class


Static methods in the parent class
Private methods in the parent class

Accessibility can not be more restricted in a child 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
}

Override annotation @Override

How to call the overridden method of a derived class?

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.

public abstract class class_name{


...
}

Often used as a parent class.


Its children will complement the un-completed parts.

Note

Abstract class can contain abstract methods

Derived class that are no abstract must implement these abstract


methods.

Abstract classes on parent class must not be declared as final or


static since child classes must implement the details of their parent's
abstract classes.
If a class has one or more abstract methods, it must be an
abstract class.

Example

abstract class Point{


private int x, y;
public Point (int x, int y){
this.x = x;
this.y = y;
}
public void move(int dx, int dy){
x += dx; y += dy;
plot();
}
public abstract void plot(); // abstract method
}
abstract class ColoredPoint extends Point{
int color;
public ColoredPoint(int x, int y, int color){
super(x,y);
this.color = color;
}
}
class SimpleColoredPint extends ColoredPoint{
public SimpleColoredPoint(int x, int y, int color){
super(x,y,color);
}
public void plot(){
//code blocks
}
}
public void main (String[] args){
Point p = new SimpleColoredPoint(a,b,red);
p.plot();
}

3. Single inheritance and multi-inheritance


Multiple Inheritance
A class can inheritance several other classes
C++ supports multiple inheritance

C++ multi-inheritance

Single Inheritance

A class can inherit only one other class.


Java supports only single inheritance.

-> Need to add the notion of Interface

Problems in Multiple Inheritance


Name clashes on attributes or operations

Repeated inheritance

Check

Implementation-dependent
4. Interface
Corresponds to different implementations
Defines the border:
What and How
Declaration and Implementation

Interface

Is an abstract "class" that is used to group related methods with "empty"


bodies.

Note

Interface allows a class to inherit (implement) multiple interfaces at the


same time.
Can not directly instantiate (khởi tạo)

Syntax

SubClass extends SuperClass implements ListOfInterfaces


//
SubInterface extends SuperInterface

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
}

Disadvantages of Interface in solving Multiple Inheritance problems

Does not provide a nature way for situations without inheritance


conflicts
Inheritance is to re-uses source code but interface can't do it

You might also like