Solved

how to call an abstract class

Posted on 2002-06-15
7
320 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
create a gui in perl 3 70
sites similar to codingbat to improve coding hanson skills 3 40
replace a word with other 1 40
even odd program using while loop 3 25
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

914 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

14 Experts available now in Live!

Get 1:1 Help Now