Author Topic:   abstract
raja pratap
greenhorn
posted April 11, 2000 04:18 AM             
I was going through RHE yesterday and I found:
A class that is abstract may not be instantiated(that is you may not call its constructor).

Now my question is.......
If we have an abstract class in which only one method is declared abstract and rest have implementation in it,then how do I call other methods with out creating an instance??

Help me out??Thanks in advance.

raja pratap
greenhorn
posted April 11, 2000 04:36 AM             
May be by creating the instance of the subclass I guess.
But if I want to use non-abstract methods with out subclassing the abstract class,what should I do??Is this possible??
Can some one give clear explanation of this concept.

Thanks again.

Gajanan
unregistered
posted April 11, 2000 06:21 AM           
Hi Pratap,
You can access non-abstract methods inside another class by
making them static.
Ex
abstract class AbClass
{ abstract public void method();
public static void amethod()
{ System.out.println("Hello");
}
}

class CheckIt
{ public static void main(String args[]){
AbClass.amethod();}//Here it is
}
If you have better solution let me know.

maha anna
bartender
posted April 11, 2000 07:12 AM             
Raja,
What Gajanan said is the solution for your qstn. But the point to understand is , the basic idea of declaring a class to be 'abstract' is you don't want an object to be created from this class. This is the meaning. An abstract class may/maynot or in fact 'need not' have an abstract method wothin the class. Do you get the point? You can have a class with ALL non-abstract methods and still can be declared as an abstract class. This feature of Java can be used intelegently in appropriate situations during the design stage of the system. You are defining a class with all non-abstract class, but for some reason you 'force' the class to be subclassed and then used. What if that kind of requirement arises?. But as you asked, if you want to use the non-abstract methods of a class means, since you CANNOT create an instance from the 'new' keyword, since all static members are called on the class as a whole (not on any particular instance of the class), by declaring the method to be static is the only way to do it.

regds
maha anna

raja pratap
greenhorn
posted April 11, 2000 06:16 PM             
Thanks alot!

|