-1

Currently i'll try to understand extended classes in java.

What was done: I create a class and extende class for him, in extended class i create constructor with simple commands - show variables from superclass. In another file i try to create object of extended class, but i have error - "No enclosing instance of type CObj is accessible. Must qualify the allocation with an enclosing instance of type CObj (e.g. x.new A() where x is an instance of CObj)."

Code: main file

public class Demo {
    public static void main (String[] args){
        CObj.Co n=new CObj.Co();
        n.show();
    }
}

and file with classes

class CObj {
    int i,k,l;
    CObj summ (CObj object){
        object.i*=i;
        object.k*=k;
        object.l*=l;
        return object;
   }
   void show (){
       System.out.println("this is super class");
       System.out.println(i+" "+k+" "+l);
   }

... few constructors... and exntended class

class Co extends CObj{
    Co(){
        super(1,2,3);
    }
    void show(){
        System.out.println("this is extended class and overloaded meth");
        super.show();
    }
}

Question; what was done wrong? why i have this error and what i need to change? Or maybe i'm doing something in a bad way?

blackpanther
  • 10,998
  • 11
  • 48
  • 78
hbk
  • 10,908
  • 11
  • 91
  • 124
  • 5
    Define each class in its own file, until you understand what inner classes are. – JB Nizet Aug 11 '13 at 09:50
  • 1
    It looks like you may have defined `Co` inside `CObj`. That doesn't do what you want. Put it in its own file instead. – user2357112 Aug 11 '13 at 09:52
  • i have 2 files for each classes - one for superclass and extended clas and one for class with main method – hbk Aug 11 '13 at 09:53
  • 2
    That makes 2 files for 3 classes. You should have 3 files: one for the main class, another one for the CObj class, and a third one for the Co class. – JB Nizet Aug 11 '13 at 09:55
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 04 '16 at 00:30

3 Answers3

4

Given your error message:

"No enclosing instance of type CObj is accessible. Must qualify the allocation with an enclosing instance of type CObj (e.g. x.new A() where x is an instance of CObj)."

It says that Co is an inner class of CObj, not a nested (static inner) class. So, you can only access Co using an instance of CObj:

Cobj.Co obj = new CObj().new Co();

But, you should be really sure that you need an inner class, while creating it. Also, as @JBNizet suggests, you should put each class in it's own file. That would make it easier to for you to understand how the classes interact. And then you can create object of Co easily using:

Co obj = new Co();

Secondly, you are trying to call the super class constructor from Co, which doesn't exist:

super(1,2,3);

You don't have any constructor in CObj, which takes 3 integer parameers. That wouldnt' compile either.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
2

Here seems to be one of your problem:

  Co(){
        super(1,2,3);
    }

You don't have such a constructor in your superclass.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • i have constructors: just remove them from post: all constructors that i have below:` CObj() { i=0; k=0; l=0; } CObj(int x) { i=x; k=0; l=0; } CObj(int x, int y) { i=x; k=y; l=0; } CObj(int x, int y, int z) { i=x; k=y; l=z; } CObj (CObj object){ i=object.i; k=object.k; l=object.l; }` – hbk Aug 11 '13 at 09:51
  • @kirill Removing unnessisary code is always a good thing but try to make sure the code is self complete – Richard Tingle Aug 11 '13 at 10:27
  • ok, next time i'll put all required code for better understandung of current problem, thanks – hbk Aug 11 '13 at 10:51
2

Your syntax is wrong here:

CObj.Co n=new CObj.Co();

you can just write:

Co obj = new Co();

Also, you don'have an (int, int, int) constructor in base class CObj, so this line in class Co is wrong:

super(1,2,3);

Whether you change the call to the existing CObj(CObj object) or you write a CObj(int a, int b, int c)

Lake
  • 4,072
  • 26
  • 36