简体   繁体   中英

calling non abstract method in abstract class java

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.

Either you create a concrete class which doesn't override the method, or within a concrete class which does override the method, you can call super.t1() . For example:

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

If you've only got an instance of an object which overrides a method, you cannot call the original method from "outside" the class, because that would break encapsulation... the purpose of overriding is to replace the behaviour of the original method.

you should be able to do it using

Test test = new Test(){};
test.t1();

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");
    }
}

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
    }
}

use keyword 'super' to do that

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

    }

Make sure you use that in the overriden method though.

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();
    }

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();

An abstract class is used when we want that every class that inherited from our abstract class should implement that abstract method, so it is must to implement method otherwise it gives the compile-time error.

void t1()
    {
        super.t1; // means the parent methods
        System.out.println("child");
    } 

For example: Bird class has method sing() and there other classes that inherited from it like the sparrow, Pigeon, Duck, so these all have sing method so we make Bird class Abstract and make the sing() method abstract in it so every child of bird that implements Bird class should have a method of sing() with its on implementation.

  1. First Create abstarct class like as shown in link: Creating Abstract Class
  2. Create Sub-Classs like as shown in link: Sub-class extending
  3. Creating main method for executing this as show in link: Instanciate the subclass to access
  4. Result as shown here: Result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM