By GokiSoft.com| 15:35 11/03/2020|
Java Basic

Share Code- Chia sẻ code java overview - Abstract Class trong java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession8;

/**
 *
 * @author Diep.Tran
 */
public abstract class Animal {
    String name, foodType;

    public Animal() {
    }

    public Animal(String name, String foodType) {
        this.name = name;
        this.foodType = foodType;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFoodType() {
        return foodType;
    }

    public void setFoodType(String foodType) {
        this.foodType = foodType;
    }
    
//    public void showSound() {
//        //Tat ca cac dong vat => deu phat ra 1 am thanh rieng
//        //cua no
//        //Noi toi dong vat chung chung => ko biet la am 
//        //thanh chung cua dong van la gi
//        //ko trien khai dc code o method nay.
//    }
    
    //bien thanh abstract method
    public abstract void showSound();
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession8;

/**
 *
 * @author Diep.Tran
 */
public class Cat extends Animal{
    public void running() {
        System.out.println("Cat is running");
    }

    @Override
    public void showSound() {
        System.out.println("Meo ... meo ...");
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession8;

/**
 *
 * @author Diep.Tran
 */
public class Dog extends Animal{

    @Override
    public void showSound() {
        System.out.println("Go ... go ...");
    }
    
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession8;

/**
 *
 * @author Diep.Tran
 */
public class Test3 {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.showSound();
        
        Cat cat = new Cat();
        cat.showSound();
    }
}


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)