Reputation: 11184
I just started to learn java, so now i read about such possibility as inheritance, so try to create class that must create object - box. And using inheritance implement new properties to created object. I try to put each class in separate file, so after creating class, try to use it in
public static void main(String[] args)
So class Inheritance:
public class Inheritance {
double width;
double height;
double depth;
Inheritance (Inheritance object){
width = object.width;
height = object.height;
depth = object.depth;
}
Inheritance ( double w, double h, double d){
width = w;
height = h;
depth = d;
}
Inheritance (){
width = -1;
height = -1;
depth = -1;
}
Inheritance (double len){
width=height=depth=len;
}
double volumeBox (){
return width*height*depth;
}
class BoxWeight extends Inheritance {
double weight;
BoxWeight (double w, double h, double d, double m){
super(w,h,d);
weight = m;
}
}
But, when i try to use BoxWeight in main class, during using i got an error
public class MainModule {
public static void main(String[] args) {
Inheritance.BoxWeight mybox1 = new Inheritance.BoxWeight(9, 9, 9, 9);
....
Error - No enclosing instance of type Inheritance is accessible. Where i'm wrong?
Upvotes: 5
Views: 224
Reputation: 4137
Problem is in this line
Inheritance.BoxWeight mybox1 = new Inheritance.BoxWeight(9, 9, 9, 9);
use
BoxWeight mybox1 = new BoxWeight(9.0, 9.0, 9.0, 9.0);
Upvotes: -1
Reputation: 3141
Inheritance.BoxWeight mybox1 = new Inheritance().new BoxWeight(9, 9, 9, 9);
You are here using both principles inheritance and inner class. Suppose that your class BoxWeight
doesn't extend Inheritance class
. Your inner class has access of the outer class some properties and methods which are object instance level attributes. So You should create new Inheritance()
then by using this instance create an instance of BoxWeight
.
Upvotes: 1
Reputation: 55589
As it stands, BoxWeight
requires an instance of Inheritance
to work (just like accessing a non-static variable or function requires an instance, so accessing a non-static inner class also does). If you change it to static
it would work, but this isn't required.
BoxWeight
doesn't need to be inside the Inheritance
class.
Instead, remove BoxWeight
out of the Inheritance
class.
And change Inheritance.BoxWeight
to BoxWeight
.
EDIT: Just for completeness, you could also make it:
Inheritance.BoxWeight mybox1 = new Inheritance().new BoxWeight(...);
Inheritance.BoxWeight
is just the type, so the above does not apply. But to create an instance of BoxWeight
, you need an Inheritance
object, which you create with new Inheritance()
.
Upvotes: 4
Reputation: 37813
If you want to keep your classes nested and not static (I don't see a good reason for it), you can also use:
Inheritance.BoxWeight mybox1 = new Inheritance().new BoxWeight(9, 9, 9, 9);
Upvotes: 1
Reputation: 4157
Ignoring the fact that this is an entirely arbitrary homework problem with crazy names, that extending a class inside the abstract class is an odd situation and that it makes no sense for a BoxWeight
to be inside an Inheritance
:
...
Inheritance i = new Inheritance(0, 0, 0, 0);
Inheritance.BoxWeight mybox1 = i.new BoxWeight(9, 9, 9, 9);
...
i
is the 'enclosing object'.
Upvotes: 0
Reputation: 162771
Change
class BoxWeight extends Inheritance
to
static class BoxWeight extends Inheritance
This should allow your code to compile. However, in addition to using the inheritance feature of java, you're also using an inner class, which isn't really necessary in this case and is likely confusing things for you. If you pull BoxWeight
out into its own file, and reference it without the Inheritance.
prefix, I think you'll find things to be easier to understand.
Upvotes: 3