How can you invoke a defined method of an abstract class?

14/04/2008

An abstract class cannot be instantiated directly. An abstract class has to be sub-classed first and then instantiated. Only then the method defined in the abstract class can be invoked.

by instantiated. do you mean declaring it? i’m new to java and i don’t like the book that we have. I’m looking at other ways to learn it.

Anonymous on January 28th, 2011 4:18 pm

A code snippet for your reference:
http://techguru.yakkoo.com/programming-concept-tutorials/2011/08/how-can-you-invoke-a-defined-method-of-an-abstract-class/

public abstract void myAbstractMethod() ;

public static void main(String[] args) {

// Not permitted
// MyAbstractClass instance1 = new MyAbstractClass ();
}
}

public class ConcreteSubclass extends MyAbstractClass {

public abstract void myAbstractMethod() {
System.out.println(“Called from concrete class”);
}

public static void main(String[] args) {

ConcreteSubclass instance1 = new ConcreteSubclass ();
instance1.myAbstractMethod();
}
}

danish on August 10th, 2011 8:58 am

in ConcreteSubclass the myAbstractMethod cannot have abstract keyword

Pradeep on March 15th, 2012 1:02 pm


Email:

about
I am Joe, author of this blog. I run this with loads of passion. If you are into java, you may find lot of interesting things around ...more about me. Google+
java badge
Home