4

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.

Dilis
  • 277
  • 2
  • 6
  • 19

9 Answers9

12

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks.I understand ... I didnt understand encapsulation part? – Dilis Dec 03 '14 at 18:59
  • @Dilis Check [why-is-super-super-method-not-allowed-in-java](http://stackoverflow.com/questions/586363/why-is-super-super-method-not-allowed-in-java) to see how it could violate encapsulation. – Pshemo Dec 03 '14 at 19:07
3

you should be able to do it using

Test test = new Test(){};
test.t1();
NikhilP
  • 1,508
  • 14
  • 23
1

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");
    }
}
Stav Saad
  • 589
  • 1
  • 4
  • 13
  • 1
    This doesn't answer the question. OP wants to know how to invoke code of method from base class on instance of derived class if derived class override this method. Base class being abstract has actually nothing to do with problem OP is describing. – Pshemo Dec 03 '14 at 18:59
1

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
    }
}
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
1

use keyword 'super' to do that

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

    }

Make sure you use that in the overriden method though.

ShreyJo10
  • 27
  • 5
0

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();
    }
Rami Del Toro
  • 1,130
  • 9
  • 24
0

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();
Praveen
  • 1,791
  • 3
  • 20
  • 33
0

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.

0
  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
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32244655) – K.Mat Jul 18 '22 at 12:40