-6

In the following example:

class A {  
    private int a;
    private int b;
    private int c;

    public A(int a, int b , int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

class B extends A {
    public B() {
         super(1,2,3);         
    }
  1. Does the statement super(1,2,3) in the class B create a private fields same as the private fields in the class A? Or is it illegal to use this statement because B cant inherit the private fields of A?
  2. And we suppose that we didn't use the super constructor in the class B then normally the computer will call the default constructor of the class A. We know that private fields are not inherited in Java so what will the default constructor initialize in this state ?
Cloo
  • 123
  • 8
  • 6
    is your code i.e. B.java compiling? – SMA Jan 06 '15 at 12:58
  • 2
    Class B doesn't even compile. – Bohemian Jan 06 '15 at 13:00
  • 3
    i think you should read about java basics first. like what is a constructor and how to use it – MihaiC Jan 06 '15 at 13:00
  • You will need that super call in B to be inside a constructor. Some more detail on inheritance regarding private members : http://stackoverflow.com/questions/4716040/do-subclasses-inherit-private-fields – GoldenJam Jan 06 '15 at 13:01
  • It is not a question of compiling or not . I had big confusion to understand it I a beginner in Java and I study these notions. I found the following [example](http://txs.io/9Fvb) with its explanation. But it's a bit confusing. – Cloo Jan 06 '15 at 13:04
  • I am not asking if it compiles or not. I am asking about an OOP notion. My question is theretical. – Cloo Jan 06 '15 at 13:04

5 Answers5

8

You cannot call super() like this:

 class B extends A {
      super(1,2,3);
   }

super() OR this() should be the first statement in a constructor. First correct this basic mistake of yours before going further. super() is used by default even if you don't explicitly use it.

class B extends A {
    B (){
       super(1,2,3);
    }
}

This is the right way. Please Read about Constructors and Java language basics first before posting questions.

EDIT

I didn't notice that someone edited you question to add super(1,2,3) in a constructor, now answering your questions as follows:

Does the statement super(1,2,3) in the class B create a private fields same as the private fields in the class A? Or is it illegal to use this statement because B cant inherit the private fields of A?

No, by calling super(1,2,3) all you're doing is passing 3 integer values to the base class constructor public A(int a, int b , int c) After that you're assigning these values to the private instance variables of base class, you're not making a separate fields for class B, if thats what you asked, and No B class still can't access base class instance variables directly (by stating directly I mean by inheritance or making an instance, there are other ways like setters/getters etc)

And we suppose that we didn't use the super constructor in the class B then normally the computer will call the default constructor of the class A. We know that private fields are not inherited in Java so what will the default constructor initialize in this state ?

No, if you don't use a constructor in B class which uses super(int, int, int) to match the arguments of base class constructor (int a, int b , int c) then your code won't even compile. The default constructor will call the no-args constructor of Base class, but since Base class has no default constructor you'll get compilation error!

Sharp Edge
  • 4,144
  • 2
  • 27
  • 41
  • There is no downvote on your answer. Maybe he misclicked, or he removed it after an edit. Who knows. – Tom Jan 06 '15 at 13:23
1

First of all you need to understand one thing: private fields of a parent class ARE BEING INHERITED. The only thing is that if they are private in parent, then they cannot be accessed directly from the child class (the B class in your example). So in other words: not a single B class method can access those fields, but every A class method can access them. So for example its possible that there is a public/protected method inside A class that changes some of those fields and this method can be called from a child class (B).

Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
  • Ok this answered all the question. Thank you very much – Cloo Jan 06 '15 at 13:09
  • Are static methods and fields inherited also ? – Cloo Jan 06 '15 at 13:12
  • But pay attention to the http://stackoverflow.com/a/27799196/1993204 answer as well. It is important, because we cannot answer a question that is (as you called) "about the notation", if the code has basic errors that are against "java convention". – Bartek Lipinski Jan 06 '15 at 13:13
  • @pentanol No static members belong to class and are not inherited. – Sharp Edge Jan 06 '15 at 13:14
  • Static members are NOT inherited but can be used in the same manner as normal members of parent object (so `private` - not directly, `public/protected` - directly). – Bartek Lipinski Jan 06 '15 at 13:29
  • Is it possible to define an object from the class B without declaring a constructor for B ? – Cloo Jan 06 '15 at 14:26
  • @pentanol You could try it first. And if you have a new question, then create own by clicking on [Ask Question](http://stackoverflow.com/questions/ask). – Tom Jan 06 '15 at 15:24
1

First of all, the code you posted is not valid Java. It is important that you post working code, otherwise we can't be sure about what you are asking.

  1. Does the statement super(1,2,3) in the class B create a private fields same as the private fields in the class A? Or is it illegal to use this statement because B cant inherit the private fields of A?

Assuming you put the statement in a constructor instead of at class level, which is illegal, then no, that will not automatically create fields in class B. It just calls the constructor in the superclass A that takes three int arguments and initializes the fields in the superclass part of the object.

  1. And we suppose that we didn't use the super constructor in the class B then normally the computer will call the default constructor of the class A. We know that private fields are not inherited in Java so what will the default constructor initialize in this state ?

Since there is no default (i.e. no-arguments) constructor in class A, you would get a compiler error - the compiler would complain that there is no appropriate constructor in class A.

Java only automatically adds a no-arguments constructor to a class if you do not specify a constructor at all in the class. Since class A already has a constructor, Java is not automatically going to add a no-arguments constructor.

Jesper
  • 202,709
  • 46
  • 318
  • 350
0

Once you correct your class to the proper:

class B extends A {
    public B() {
        super(1,2,3);
    }        
}

...we can proceed to answer your actual questions.

The constructor of A does not create fields. The fields are created as part of creating of any A object, and are initialized by the constructor.

Those fields are created in B as well, but not because you called super(1,2,3) but because you extend A. As soon as an instance of B, which is an extended instance of A is created, those fields are there - but they are accessible only to methods that are declared in A itself and not in its descendents.

By calling super(1,2,3), you are initializing those private fields. They are still not accessible to B. The constructor of A is mediating between B and these private variables. If you had a method that prints those fields in A, and that method was not private, you could call it and it would print them with these values.

As for your second question, if you didn't call the super(1,2,3), Java would try to call the default constructor. However, for a class that has a constructor, there is no default constructor. The empty/nullary constructor only exists if you either declared it yourself, or if you didn't declare any constructor at all.

// The following two classes have a default constructor which will be called
// if any descendent doesn't call super(...)

class HasADefaultConstructor {
}

class AlsoHasADefaultConstructor {

    AlsoHasADefaultConstructor() {
    }
}

// But this one doesn't.

class DoesntHaveADefaultConstructor {

    DoesntHaveADefaultConstructor( String a ) {
    }
}
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
0

So you can't inherit private varibles, but you can access them if the parent class has the appropriate getters: Run the example and you see the output is 'a is: 1' Regarding the default constructor: In your example you have explicitly implemented a constructor. So the implicit default constructor is not there any more

class B extends A {
    public B() {
        super(1, 2, 3);
    }

    public void foo() {
        System.out.println("a is: " + super.getA());
    }

    public static void main(String[] args) {

        B bb = new B();
        bb.foo();

    }

}


class B extends A {
    public B() {
        super(1, 2, 3);
    }

    public void foo() {
        //access a
        System.out.println("a is: " + super.getA());
    }

    public static void main(String[] args) {

        B bb = new B();
        bb.foo();

    }

}
ProgrammingIsAwsome
  • 1,109
  • 7
  • 15