Lớp trừu tượng (abstract class) và hàm trừu tượng (abstract method)
Ví dụ 1:
package com.nentang.vn;
public abstract class ConVat {
public String ten;
public int tuoi;
public boolean gioi_tinh;
public float so_chan;
public float can_nang;
abstract void Keu();
public void chay_nhay() {
System.out.println("ConVat dang chay nhay!");
}
}
public class Cho extends ConVat {
@Override
public void Keu() {
System.out.println("Gau gau!");
}
}
public class Meo extends ConVat {
@Override
public void Keu() {
System.out.println("Meoo meoo!");
}
}
Ví dụ 2:
abstract class Bike {
abstract void run();
}
class Honda extends Bike {
void run() {
System.out.println("running safely..");
}
public static void main(String args[]) {
Bike obj = new Honda();
obj.run();
}
}
|