Solved

how to call an abstract class

Posted on 2002-06-15
7
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

622 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