Java learning notes - abstract classes

Recently, I'm learning Java. Let's talk about the understanding of abstract classes in learning. If there are mistakes and, please correct them.                

There are subclasses and superclasses in Java inheritance. Superclass and subclass can be said to be the relationship between general and special, abstract and concrete. For example, fruits and bananas, people and students, cars and red flags, mobile phones and Huawei, etc.           

Take Dog class and Bird class for example here. Each Animal will have its own name or species. Therefore, we can introduce a public superclass and put the duplicate methods in Dog class and Bird class at a higher level of inheritance level. In addition, we can add a getDescription method to the public superclass, which can briefly describe an Animal. At this point, we set up a super class Animal.

However, in Animal, you only know the name of the Animal and know nothing about other descriptions. How to set the getDescription method? At this time, you can use the abstract keyword, so you don't need to implement this method at all, and leave the specific implementation code to the subclass.

public abstract class Animal
{
    /*.....*/
    public abstract void getDescription();//Abstract method
}

In order to improve the clarity of the program, the class itself containing one or more abstract methods must be declared abstract. For example, in Animal, if the getDescription() method is abstract, the class must also be declared abstract.

In addition to abstract methods, abstract classes can also contain some specific fields and specific methods.

public abstract class Animal
{
    private String name;
    public Animal(String _name)
    {
        this.name=_name;
    }
    public String getName()
    {
        return this.name;
    }
    public abstract void getDescription();//Abstract method
}

The Dog and Bird classes are defined by extending the abstract Animal class and implementing the method getDescription class. At this time, since there are no abstract methods in the Dog and Bird classes, it is not necessary to declare them as abstract classes.

You can declare a class as an abstract class even if it does not contain abstract methods.

An abstract class cannot be instantiated, but an object of a concrete subclass can be created.

Animal dog1 = new Animal("Xiao Hei");//Incorrect declaration

Animal dog1 = new Dog("Xiao Hei","Woof, woof","Alaska");
//dog1 here is an abstract type of Animal variable, which refers to an instance of a non Abstract subclass dog

Here is the test code.

public abstract class Animal
{
    private String name;
    public Animal(String _name)
    {
        this.name=_name;
    }
    public String getName()
    {
        return this.name;
    }
    public abstract void getDescription();//Abstract method
}
public class Bird extends Animal
{
    private String color;
    private int age;
    public Bird(String _name,String _color,int _age)
    {
        super(_name);
        color = _color;
        age = _age;
    }
    @Override
    public void getDescription()
    {
        System.out.println("Age is"+age+color+"Colored"+getName()+"Flying in the air");
    }
}
public class Dog extends Animal
{
    private String call;
    private String kind;
    public Dog(String _name,String _call,String _kind)
    {
        super(_name);
        call=_call;
        kind=_kind;
    }
    @Override
    public void getDescription()
    {
        System.out.println(getName() + "The type of is"+kind+"sure" + call);
    }
}
public class Test
{
    public static void main(String[] args)
    {
        var animal = new Animal[2];
        animal[0] = new Dog("Ha","Woof, woof","Siberian Husky");
        animal[1] = new Bird("Xiao Hong","red",5);

        for(Animal a : animal)//Using abstract methods, you can call getDescription directly from variable a.
            a.getDescription();
       
    }
}

 

Tags: Java abstract class Class

Posted by Andre_Ng on Sat, 29 Jan 2022 07:59:34 +1030