Solved

how to call an abstract class

Posted on 2002-06-15
7
324 Views
Last Modified: 2010-03-30
Hai

I want to know any way to call an abstract class by using Class c=Class.forName(abstract class);
0
Comment
Question by:hellohowareu
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 7080377
What would you want to do with 'c' as in Class c?
0
 
LVL 1

Accepted Solution

by:
flooder earned 100 total points
ID: 7080763
As I understand you can only inherit from a abstract class. The purpose of it is to define a structure, to which the other classes that inherit from it must comply to. So my answer would be is that you can't call a abstract class and wanting to do so would point to a flaw in design.
0
 
LVL 92

Expert Comment

by:objects
ID: 7080940
You can certainly create a Class instance for an abstract class, eg.

Class cls = Class.forName("java.awt.Graphics");

But you cannot create instances of an abstract class using any means.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 7083237
Hi,

You need to use Class.forName() only if you don't know class name at compile time. If you really need Class instance, you can use ClassName.class construct to get it. Depending on what you need, Class instance might be not necessary.

You can create instances of abstract classes, but this classes can be used to declare variables, method parameters etc.:

abstract class A{
    public anstract void someMethod();
    public void anotherMethod(){
    }
}

class B extends A{
    public void someMethod(){
    }
}

class User{
    A field;
    A methodReturningA(){
        return new B();
    }
    void methodReceivingA(A parameter){
        parameter.someMethod();
        parameter.anotherMethod();
    }
    void instanceofCheck(Object supposedToBeA){
        if( supposedToBeA instanceof A){
            A var = (A)supposedToBeA;
            var.someMethod();
        }
    }
}

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
0
 
LVL 4

Expert Comment

by:kylar
ID: 7084481
This is exactly what you wanted to know in your previous question.

http://www.experts-exchange.com/java/Q_20310654.html

As Objects and I both said, you can't instantiate an abstract class. Reflection or standard, it just won't work. You need to get a concrete subclass of the abstract class (make one up if it doesn't exist). then you can create it and reflect it as needed.

Cheers,
Kylar
0
 
LVL 4

Expert Comment

by:kylar
ID: 7084491
Also, you have yet to close a question since joining, and have open, unlocked questions dating back 2.5 years.. It is bad etiquette not to follow up on your questions.

Cheers,
Kylar
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7121670
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question