Solved

how to call an abstract class

Posted on 2002-06-15
7
322 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
backtracking recursion  code 19 57
Java DateChooser? 3 36
javap bin 2 34
Running JavaFX on JDeveloper 12C 1 31
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

813 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

10 Experts available now in Live!

Get 1:1 Help Now