constructors in java - java

public class A {
public A() {
System.out.println("a1");
}
public A(int x) {
System.out.println("a2");
}}
public class B extends A {
public B() {
super(5);
System.out.println("b1");
}
public B(int x) {
this();
System.out.println("b2");
}
public B(int x, int y) {
super(x);
System.out.println("b3");
}}
I don't understand why the default constructure of A is not applied when I run B b= new B();
B extends A, so First we call the constrcture of A that supposed to print "a1", and then we call the the second constructure of A which prints "a2" and B() prints "b1", but when I run it, it prints only "a2,b1", so obviously A() wan't applied at the beginning- why?

When you say B b = new B(); you are calling the default constructor which is
public B() {
super(5);
System.out.println("b1");
}
Since this already has a call to its super constructor [super(5)] so the compiler will not insert the implicit default constructor. Hence the result.
NOTE: From your question, it seems that you have the idea that all the constructors are called when you create an object. I am afraid that is incorrect. Only that constructor will be called which you explicitly call to create the object. And if that constructor calls other constructor via the this() method, only then the other constructors will be called.

B extends A, so First we call the constrcture of A that supposed to print "a1"
This statement is incorrect.
In class B your no arguments constructor
public B() {
super(5);
System.out.println("b1");
}
calls the constructor of the superclass (class A) which takes an int parameter.
public A(int x) {
System.out.println("a2");
}
You never make a call to super() so the constructor that prints "a1" will not be called when you call any of B's constructors
Calling a super constructor must be the first line of a constructor. If you wish to call the no argument constructor of a superclass (in this case, the one that prints "a1"), you would write...
public B() {
super();
System.out.println("b1");
}
If you do not specify calling a super constructor, then java will automatically put in a call to the no argument super constructor.

That's because you call super(5) in B constructor which call the second A constructor instead of the first one.

I think you missunderstand how Java handles constructors. First of all, by default Java will call only one constructor per class, unless you explicitly tell it to call more using this(...). Secondly, that one construct that is called is the default constructor of the super class (so it will call super(), not this()); so class A actually looks like this:
public class A {
public A() {
super(); // implicit call to super(), which is Object()
System.out.println("a1");
}
public A(int x) {
super(); // implicit call to super(), which is Object()
System.out.println("a2");
}
}
So invoking A() will implicitly call Object(), however invoking A(int x) will also implicitly call Object(), and not - how you seem to assume - A().
Since in B you always explicitly specify to call another constructor, the compiler will not add anything. Below I added comments on what will happen on the calls to super(...) and this(...)
public class B extends A {
public B() {
super(5); // explicit call to A(5), no implict call to A()
System.out.println("b1");
}
public B(int x) {
this(); // explicit call to B(), no implicit call to A()
System.out.println("b2");
}
public B(int x, int y) {
super(x); // explict call to A(x), no implicit call to A()
System.out.println("b3");
}
}
So again, the important thing to remember is that Java will insert a call to super() in the first line of any constructor, unless you explicitly invoke another constructor using this(...) or super(...). It will never insert this() by itself.

Related

When is a constructor inherited by subclass

public class A {
public A() {
System.out.println("A");
}
}
public class B extends A{
public B() {
System.out.println("B");
}
}
public static void main(String[] args){
B b1 = new B();
Output:
A
B
So what's confusing me is, the Inheritance documentation of Java states that:
Constructors are not members, so they are not inherited by subclasses,
but the constructor of the superclass can be invoked from the
subclass.
From my understanding of that, unless you specifically call for super() in the constructor of class B, it should not print A.
So the question is, why does it print A?
The compiler calls the default constructor (no-arg constructor) of the superclass initially from the subclass constructor. So you don't need to explicitly call it. That's why the line is getting printed above.
If you want to call non-default constructor (constructor with arguments) of superclass, then you would have to explicitly call it form subclass.

Use of super keyword in Java

I have doubt regarding printing of superclass constructor statement even when I have not used super() keyword in subclass.
class A
{
int i;
A()
{
System.out.println("A's constructor");
}
}
class B extends A
{
int i;
B(int a , int b)
{
super.i=a;
i=b;
}
void show()
{
System.out.println(super.i);
System.out.println(i);
}
}
class UseSuper
{
public static void main(String[] args)
{
B b=new B(1,2);
b.show();
}
}
The output of my program is:
A's constructor
1
2
I am unable to understand why I am getting A's constructor printed on my console?
Check the following lines from https://docs.oracle.com/javase/tutorial/java/IandI/super.html
Note: If a constructor does not explicitly invoke a superclass
constructor, the Java compiler automatically inserts a call to the
no-argument constructor of the superclass. If the super class does not
have a no-argument constructor, you will get a compile-time error.
Object does have such a constructor, so if Object is the only
superclass, there is no problem.
If a subclass constructor invokes a constructor of its superclass,
either explicitly or implicitly, you might think that there will be a
whole chain of constructors called, all the way back to the
constructor of Object. In fact, this is the case. It is called
constructor chaining, and you need to be aware of it when there is a
long line of class descent.
I hope, it clears your doubts.
[Update]
Posting this update to clear OP's doubts he has mentioned in his comment below.
The following code won't compile because implicit super constructor A() has not been defined and we have also not defined it explicitly. Note that the implicit super constructor A() is automatically defined when there is no other constructor with arguments have been defined.
class A {
int i;
A(int x,int y){
}
}
class B extends A {
int i;
B(int a, int b) {
super.i = a;
i = b;
}
void show() {
System.out.println(super.i);
System.out.println(i);
}
}
public class UseSuper {
public static void main(String[] args) {
B b = new B(1, 2);
b.show();
}
}
[Another Update]
Posting this update to clear OP's another doubt which he has mentioned in his comment below.
The following code too won't compile because the super constructor A() has been declared as private preventing the child class constructor to call it.
class A {
int i;
private A() {
System.out.println("A's constructor");
}
}
class B extends A {
int i;
B(int a, int b) {
super.i = a;
i = b;
}
void show() {
System.out.println(super.i);
System.out.println(i);
}
}
class UseSuper {
public static void main(String[] args) {
B b = new B(1, 2);
b.show();
}
}
When a class extends another class, it is crucial to first call the constructor of the parent class and initialize it, before calling the constructor of the current class.
Even when not visually calling super() in any part of your constructor, Java itself calls the constructor of class A.
If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the superclass does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
In other words, constructor B(int a , int b) calls constructor A() implicitly. In IDE, just change A() to A(int i) and you'll see an error message for constructor B(int a , int b) like "Implicit super constructor A() is undefined. Must explicitly invoke another constructor".

Creating same method in both Parent and Child class provides some weird output

In the below code , I have three questions. I know that having same function in both Parent and Child class does not make any sense and not at all a good design. But , since Java is allowing me to do this , it was possible to write the below code. Trying to understand what actually happens under the hood.
1.When I call f() from the constructor of class A I found that it is calling the child class f(). This is an expected behaviour. But , when the parent constructor calls the overloaded f() before initialising the child class members ,why “B” is getting printed.
2.Why I have two values for a final variable X (x=null , x=B)?.
class A{
A(){
System.out.println("A's Constructor");
f();
}
void f() {System.out.println("A");}
}
class B extends A{
B(){
System.out.println("B's Constructor");
f();
}
final String x = "B";
void f() {System.out.println(x);}
}
public class JavaPOCSamples {
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println("Java POC");
new B();
}
}
Output as below when “B”.trim() is used in the above code:
A's Constructor
null // Note that a final variable X is null
B's Constructor
B // Note that a final variable X is changed to "B"
It is exactly the expected behaviour and is exactly why you should never call overridable methods from a constructor.
The constructor of B first calls the constructor of A, that calls f(), (from class B because it's overridden), that prints x, which is still null. Then B sets it's initialized member variables (so x is no longer null), prints something and calls f() again...
That's is because of the Java Language Specification of Inheritance. Here is a quote from Oracle Java docs.
With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called.
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
Small & Simple Explanation:
super() means the constructor (equivalent) of immediate parent class.
class A{
A(){
System.out.println("A's Constructor");
f();
}
// overloaded constructor
// calling super(int a) in child class will call this
A(int a){
System.out.println("A's Constructor " + a);
f();
}
void f() {System.out.println("A");}
}
Specs means, in an inheritance, when an object of the Child Class is created/instantiated, the Object of the Parent Class must instantiate first. So by default super() is invoked always even if you don't invoke it explicitly (any of the overloaded super(arg...) method) in your code.
If you call an overloaded super(args...) of your parent class from your child class, then the default super() is not called.
That means, in your code, Java Compiler puts the default line of super() at very first line in the child constructor, when you don't invoke any of super constructors.
Practical Proof ?? (in your code..??)
class A{
A(){
System.out.println("A's Constructor");
f();
}
// overloaded constructor
// calling super(int a) in child class will call this
A(int a){
System.out.println("A's Constructor " + a);
f();
}
void f() {System.out.println("A");}
}
class B extends A{
B(){
// super(); java adds this line when you don't put any super(args...) call
System.out.println("B's Constructor");
f();
}
B(int a){
super(a);
System.out.println("B's Constructor "+ a);
f();
}
final String x = "B".trim();
void f() {System.out.println(x);}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println("Java POC");
new B(5);
}
}
Output: (Read comments for some hints..)
A's Constructor 5
null
B's Constructor 5
B
try to play with super() calls.. and get more questions to learn Java deeper. :)
Cheers!!
Recommended reads...
https://stackoverflow.com/a/3767389/6446770
https://stackoverflow.com/a/3767391/6446770
https://stackoverflow.com/a/3767421/6446770

Constructor of a derived class (Java)

I'm very new in Java, and about to ask a fundamental question. Hope you guys could help me. Supposed I have a base classe Super and a derived class Sub, which inheritances from class Super as follows:
public class TestSuperSub {
public static void main(String[] args) {
Super ou = new Sub(5,10);
}
}
class Super {
Super() {
System.out.println("Super()");
}
Super(int x, int y) {
System.out.println("Super(int, int)");
}
}
class Sub extends Super {
public Sub(int x, int y) {
System.out.println("Sub(int, int)");
}
}
The output is
Super()
Sub(int, int)
I understand, that ou calls Sub::Sub(int,int) and therefore, Sub(int, int) is printed. But why is Super() printed, since Super::Super() hasn't never been called?
Could someone please explain it to me.
Thanks a lot!
Cheers
By default, Java will call the no-arg constructor of a super class unless you explicitly call another constructor. If you want to call Super(int, int), you must call it explicitly:
public Sub(int x, int y) {
super(x, y);
System.out.println("Sub(int, int)");
}
But why is Super() printed, since Super::Super() hasn't never been called?
It has, because your Sub constructor is implicitly calling it. It's as if you'd written:
public Sub(int x, int y) {
super();
System.out.println("Sub(int, int)");
}
From section 8.8.7 of the JLS:
The first statement of a constructor body may be an explicit invocation of another constructor of the same class or of the direct superclass (§8.8.7.1).
...
It is a compile-time error for a constructor to directly or indirectly invoke itself through a series of one or more explicit constructor invocations involving this.
If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.
If you want to call a different superclass constructor, you need to do that implicitly. When you call a subclass constructor, that will always work its way up through the inheritance hierarchy, executing the body of the superclass constructor before the subclass constructor... indeed, even field initializers in the subclass are only run after the superclass constructor.
Its the very basic fundamental concept in Java. Its the magic of super keyword and constructor chaining.
Go through these links. Hope this would help you understand the concept.
http://docstore.mik.ua/orelly/java-ent/jnut/ch03_04.htm
http://docs.oracle.com/javase/tutorial/java/IandI/super.html
By Default in constructor of sub class
the first line is call base class default constructor (implicitly) if no constructor is mentioned that is **
super();
**
if you write
super(x,y);
then other constructor will be called
Note : First line of constructor is to call base class constructor.
if there is no super class then Object class's constructor is called
When you instantiate a class, all the superclass hierarchy must also be instantiated and it will be done so through the null constructors automatically available for each class.
The following code
public class Superclassing {
public static void main(String[] args) {
new C();
}
Superclassing() { System.out.println("super"); }
}
class A extends Superclassing {
A() { System.out.println("A"); }
}
class B extends A {
B() { System.out.println("B"); }
}
class C extends B {
C() { System.out.println("C"); }
}
outputs
super
A
B
C
It is done so by design as mentioned in Skeet's answer and also here (oddly, it is mentioned before it is explained in 8.8.7):
JLS 8.8.3. Constructor Modifiers
The lack of native constructors is an arbitrary language design choice
that makes it easy for an implementation of the Java Virtual Machine
to verify that superclass constructors are always properly invoked
during object creation.
(emphasis mine.)
class A{
A(){
System.out.println("no-arg constructor in A");
}
A(int a){
System.out.println("parameterized constructor in A");
}
}
class B extends A{
B(){
System.out.println("no-arg constructor in B");
}
B(int b){
//by default during compilation super() keyword will be added in first line of this constructor
System.out.println("paramterized constructor in B");
}
}
public class TestDemo {
public static void main(String[] args) {
B aVar = new B(10);
aVar = new B();
}
}
Output:
// output for first object i.e, new B(10)
no-arg constructor in A
paramterized constructor in B
//output for second object i.e, new B()
no-arg constructor in A
no-arg constructor in B
A super() keyword will be add by default by the compiler during compilation depending on the Object that u create in main method.

Polymorphism and Constructors

I am an AP Java Student and I am practicing for my exam. I came across this question and I don't understand the answer:
Consider the following classes:
public class A
{
public A() { methodOne(); }
public void methodOne() { System.out.print("A"); }
}
public class B extends A
{
public B() { System.out.print("*"); }
public void methodOne() { System.out.print("B"); }
}
What is the output when the following code is executed:
A obj = new B();
The correct answer is B*. Can someone please explain to me the sequence of method calls?
The B constructor is called. The first implicit instruction of the B constructor is super() (call the default constructor of super class). So A's constructor is called. A's constructor calls super(), which invokes the java.lang.Object constructor, which doesn't print anything. Then methodOne() is called. Since the object is of type B, the B's version of methodOne is called, and B is printed. Then the B constructor continues executing, and * is printed.
It must be noted that calling an overridable method from a constructor (like A's constructor does) is very bad practice: it calls a method on an object which is not constructed yet.
The base class must be constructed before the derived class.
First A() is called which calls methodOne() which prints B.
Next, B() is called which prints *.

Categories

Resources