Invoking non abstract methods in abstract class java

I have three classes This seems to be a basic problem But I can't find the answer through Google search

public abstract class Test {

    void t1()
    {
        System.out.println("super");

    }

}
 public class concret extends Test{

    void t1()
    {
        System.out.println("child");

    }
    void t2()
    {
        System.out.println("child2");

    }

}

public class run {
    public static void main(String[] args) {
        Test t=new concret();

        t.t1();
    }

}

How to call abstract class T1 methods? Since I can't create objects from abstract classes, how do I call T1 in abstract classes? thank you.

Solution

Either create a concrete class that does not override the method, or in a concrete class that overrides the method, you can call super t1(). For example:

void t1()
{
    super.t1(); // First call the superclass implementation
    System.out.println("child");
}

If you have only one object instance of the override method, you cannot call the original method from the "outside" of the class, because it will destroy the encapsulation... The purpose of the override is to replace the behavior of the original method

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>