Inheritance for beginners

I was just starting to learn Java, so now I read about a feature like inheritance, so try creating a class that should create a box object. And using inheritance implements new properties for the created object. I am trying to put each class in a separate file, so after creating the class try using 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 the main class, during use I got an error

public class MainModule {
    public static void main(String[] args) {
    Inheritance.BoxWeight mybox1 = new Inheritance.BoxWeight(9, 9, 9, 9);
....

Error. Access to an instance of type Inheritance is not available. Where am I mistaken?

+5
source share
6 answers

, BoxWeight Inheritance ( , , , ). static, , .

BoxWeight Inheritance.

BoxWeight Inheritance.

Inheritance.BoxWeight BoxWeight.

: :

Inheritance.BoxWeight mybox1 = new Inheritance().new BoxWeight(...);

Inheritance.BoxWeight - , . BoxWeight Inheritance, new Inheritance().

+4

class BoxWeight extends Inheritance

static class BoxWeight extends Inheritance

. , java, , , , . BoxWeight Inheritance., , , .

+3

, , ( ), :

Inheritance.BoxWeight mybox1 = new Inheritance().new BoxWeight(9, 9, 9, 9);
+1
Inheritance.BoxWeight mybox1 = new Inheritance().new BoxWeight(9, 9, 9, 9);

. , BoxWeight Inheritance class. , . new Inheritance(), , , BoxWeight.

+1

Ignoring the fact that this is a completely arbitrary problem with homework with crazy names, extending a class inside an abstract class is an odd situation and that it makes no sense to BoxWeightbe inside Inheritance:

   ...
   Inheritance i = new Inheritance(0, 0, 0, 0);
   Inheritance.BoxWeight mybox1 = i.new BoxWeight(9, 9, 9, 9);
   ...

i is a "covering object".

0
source

The 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);
-1
source

All Articles