calling non abstract method in abstract class java

2020-07-22 17:23发布

I have 3 classes. It seems basic question. But I can'nt find answer by googling.

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 do I call abstract class t1 method? Since I cant create object from abstract class how do I call t1 in abstract class? Thank you.

标签: java
8条回答
祖国的老花朵
2楼-- · 2020-07-22 17:30

See following tests:

public abstract class BaseClass {

    public void doStuff() {
        System.out.println("Called BaseClass Do Stuff");
    }

    public abstract void doAbstractStuff();
}

public class ConcreteClassOne extends BaseClass{

    @Override
    public void doAbstractStuff() {
        System.out.println("Called ConcreteClassOne Do Stuff");
    }
}

public class ConcreteClassTwo extends BaseClass{

    @Override
    public void doStuff() {
        System.out.println("Overriding BaseClass Do Stuff");
    }
    @Override
    public void doAbstractStuff() {
        System.out.println("Called ConcreteClassTwo Do Stuff");
    }
}

public class ConcreteClassThree extends BaseClass{

    @Override
    public void doStuff() {
        super.doStuff();
        System.out.println("-Overriding BaseClass Do Stuff");
    }
    @Override
    public void doAbstractStuff() {
        System.out.println("Called ConcreteClassThree Do Stuff");
    }
}

public class Test {

    public static void main(String[] args) {
        BaseClass a = new ConcreteClassOne();
        a.doStuff(); //Called BaseClass Do Stuff
        a.doAbstractStuff(); //Called ConcreteClassOne Do Stuff

        BaseClass b = new ConcreteClassTwo();
        b.doStuff(); //Overriding BaseClass Do Stuff
        b.doAbstractStuff(); //Called ConcreteClassTwo Do Stuff

        BaseClass c = new ConcreteClassThree();
        c.doStuff(); //Called BaseClass Do Stuff
                        //-Overriding BaseClass Do Stuff
        c.doAbstractStuff(); //Called ConcreteClassThree Do Stuff
    }
}
查看更多
等我变得足够好
3楼-- · 2020-07-22 17:34

use keyword 'super' to do that

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

    }

Make sure you use that in the overriden method though.

查看更多
贼婆χ
4楼-- · 2020-07-22 17:37

Your code seems to call t1(). However this is calling the concrete t1() because the abstract t1() has been overridden by the concrete class.

If you wish to call the abstract t1 method from main code, do not override the t1() in concrete.

Or you can create a method in the concrete class for example:

    public void invokeSuperT1(){
      super.t1();
    }
查看更多
戒情不戒烟
5楼-- · 2020-07-22 17:41

Abstract class means the class has the abstract modifier before the class keyword. This means you can declare abstract methods, which are only implemented in the concrete classes.

For example :

public abstract class Test {
     public abstract void foo();
}

public class Concrete extends Test {
    public void foo() {
        System.out.println("hey");
    }
}
查看更多
一纸荒年 Trace。
6楼-- · 2020-07-22 17:44

Create an anonymous Inner class,

Abstract class:

 abstract class  Test{
        abstract void t();
        public void t1(){
            System.out.println("Test");
        }
    }

Here is how to create anonymous inner class:

Test test = new Test() {

            @Override
            void t() {
                //you can throw exception here, if you want
            }
        };

Call the class via the object created for abstract class,

test.t1();
查看更多
Summer. ? 凉城
7楼-- · 2020-07-22 17:45

you should be able to do it using

Test test = new Test(){};
test.t1();
查看更多
登录 后发表回答