Sponsored Link •
|
Advertisement
|
Advertisement
|
This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.
Message:
Hi John I hope you will get an idea about how an abstract class or interface can be used without instantiating them, by seing my example. Actually "myAudioClip" or "in" is not instantiated directly (They can't be) but an instance of their sub class. abstract class MyAbstract{ class AbstractContainer{ static MyAbstract my_abstract; static{ class AbstractImplement extends MyAbstract{ void abstractMethod(){ System.out.println("ABSTRACT METHOD CALLED"); class Test{ public static void main(String ss[]){ AbstractContainer.my_abstract.abstractMethod(); Think Test as our class that is invoking an abstract method abstractMethod() in MyAbstract class. See another class AbstractContainer has a variable of type MyAbstract. Inside the static block my_abstract is referencing to an instance of it's sub class. This sub class AbstractImplement is implementing abstractMethod(). This casting is possible in Java. Now the variable my_abstract has a reference to a "concrete" class. Test class doesn't have any knowledge about this implementing class. Replies: |
Sponsored Links
|