Calling an abstract method in an abstract java class
I have 3 classes. Seems to be the main question. But I cannot find an 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 the method of the abstract class t1? Since I cannot create an object from the abstract class, how can I call t1 in the abstract class? Thank.
Either you create a concrete class that does not override the method, or inside 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 only have an instance of an object that overrides a method, you cannot call the original method from the class "outside", because that will break the encapsulation ... the purpose of overriding is to replace the behavior of the original method.
See the 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
}
}
Create an anonymous inner class,
Abstract class:
abstract class Test{
abstract void t();
public void t1(){
System.out.println("Test");
}
}
Here's how to create an anonymous inner class:
Test test = new Test() {
@Override
void t() {
//you can throw exception here, if you want
}
};
Call the class through the object created for the abstract class,
test.t1();
use the 'super' keyword for this
void t1()
{ super.t1();
System.out.println("child");
}
Make sure you use this in your overriden method.
An abstract class means that the class has a modifier abstract
before the keyword class
. This means that you can declare abstract methods that are only implemented in concrete classes.
For example:
public abstract class Test {
public abstract void foo();
}
public class Concrete extends Test {
public void foo() {
System.out.println("hey");
}
}
Your code seems to be calling t1 (). However, this calls a specific t1 () because the abstract t1 () has been overridden by a specific class.
If you want to call the abstract method t1 from your main code, don't override t1 () in concrete.
Or you can create a method in a concrete class like:
public void invokeSuperT1(){
super.t1();
}