The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2001

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:

Hiding the implementation

Posted by Jinaraj on January 15, 2001 at 4:47 AM

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.
You can always cast a reference of a sub class object to a super class variable.

abstract class MyAbstract{
abstract void abstractMethod();
}

class AbstractContainer{

static MyAbstract my_abstract;

static{
my_abstract=new AbstractImplement();
}
}

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.
Thus implementation of the methods in an interface or an abstract class is hidden from the user.



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us