1

I have an interface

public interface I{
  Status getStatus();
}

then I have an abstract class implementing that interface

public abstract class C implements I{
       public Status getStatus() {
        return status;
    }
}

I want to access the getstatus() method from another class, I have tried

C status = new C();

but I am getting error "Cannot instantiate the type C"

Any help will be highly appreciated..!! thanx.

3
  • 2
    You've got an abstract class which implements an interface. You just can't instantiate it because it's abstract - which has nothing to do with the fact that it implements an interface. Commented Sep 6, 2013 at 5:52
  • so, how can I access the getStatus method then? Commented Sep 6, 2013 at 5:54
  • 1
    You have to instantiate a concrete subclass of C, which is the normal behaviour for abstract classes. (Note that you'd need to declare a status variable in C for C to even compile, of course.) Commented Sep 6, 2013 at 5:55

5 Answers 5

4

You can not create object for an abstract class Write a concrete class which extends your abstract class and use that class object to call the method

 class test extends c{
      ..........
     }

    c  obj1= new test();
   obj1.getStatus();
Sign up to request clarification or add additional context in comments.

4 Comments

... but give it a name which doesn't violate Java naming conventions, of course. And don't declare the variable to be of type Class, as that won't compile.
@Looser test extends c which implements I So you need to add unimplemnted methods
@javaBeginner That method was already implemented in abstract level so doesn't required,if there are any unimplemented at abstract level you should implement them in test
@Looser After I saw your code,I tried In my PC,It shows compilation error
4

According to docs

A class type should be declared abstract only if the intent is that subclasses
can be created to complete the implementation. If the intent is simply to prevent
instantiation of a class, the proper way to express this is to declare a 
constructor of no arguments, make it private, never invoke it, and declare no
other constructors.

Abstract class can have not abstract methods but it must have a valid use case(like calling super from subclass). You cannot instantiate(create objects of abstract class).

So either remove abstract keyword or create another class that extends your abstract class.

Ans just for the record when an abstract class implements an interface you need not implement the interface methods in abstract class(though you can if your design dictates so). But in case you don't implement interface methods in the abstract class implementing it you need to implement the same in first concrete subclass of your abstract class. Also if you do implement you interface methods in abstract class then no need to implement them again in the concrete subclass of the abstract class. You can always override it though.

Comments

2

You can not instantiate abstract class

2 Comments

So how can I access the getStatus() method then?
@AbhishekGoel its better you read this tutorial.
2

Assuming that your class must be abstract.

You cannot instantiate an abstract class.

What you can do is,Let any child to extends that class and create object for that.

Abstract Methods and Classes

2 Comments

Since you cannot create instance it directly,create a child(subclass) and create object for that
Glad it helped :) Go through the example I linked from official docs
0

If you really abstract methods in class C, then I recommend simply using a regular class. You can still extends that calls as well.

1 Comment

Hi and welcome to S/O! :) This would be better off as a comment, rather than as an answer - as you're not actually an swering the question. I would recommend reading How do I write a good answer?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.