Solved

how to call an abstract class

Posted on 2002-06-15
7
318 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
Comment Utility
What would you want to do with 'c' as in Class c?
0
 
LVL 1

Accepted Solution

by:
flooder earned 100 total points
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 7

Expert Comment

by:Igor Bazarny
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now