Java Abstract Class Example

Java abstract class is a class that is declared as abstract, it cannot be instantiated, which means you cannot create an instance of abstract class. The only way to instantiate is through its child classes. A class extends from the abstract class can be instantiated and uses the properties and methods from the abstract class. Abstract class can define abstract methods, those methods are declared but no code are implemented. The child class must either override the abstract method or declare itself abstract.

An abstract class example

public abstract class Animal {
    
    private String breed;
    
    // constructor method
    public Animal(String b)   
    {
        breed=b;
    }
    
    // regular getter method
    public String getBreed()    
    {
        return breed;
    }
    
    // abstract method has no implementation
    public abstract void describe(); 

}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search