Java: Why does my class automatically inherits constructor from superclass? [duplicate] - java

This question already has answers here:
Inheritance in Java - creating an object of the subclass invokes also the constructor of the superclass. Why exactly?
(15 answers)
Default constructors and inheritance in Java
(11 answers)
order of constructor calls in multilevel inheritance in java [duplicate]
(4 answers)
Closed 8 years ago.
When I try the code on the bottom of this question, the output is:
a a b a b c
So this means that the constructor from B and C call the constructors from their superclasses. But why?
I thought that the superclass' constructor only get's called when used with the super() function like this:
public sportscar(String name, int weight, int topspeed){
super(name, weight);
this.setTopspeed(topspeed);
}
But if it automatically takes over the constructor from the classes they extend from, why would we use the super() function? I know that normal methods extend to there sub-classes automaticaly, but I thought that the constructor was different.
If anyone can clear this out for me, thanks a lot!
Code:
public class App {
public static void main(String[] args) {
new A();
new B();
new C();
}
}
public class A {
public A(){
System.out.println("a ");
}
}
public class B extends A {
public B(){
System.out.println("b ");
}
}
public class C extends B {
public C(){
System.out.println("c ");
}
}

This behaviour is mandated by the JLS (ยง8.8.7. Constructor Body):
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.

Related

Why does the constructor of a superclass automatically execute when i create a subclass in main()? [duplicate]

This question already has answers here:
Is it unnecessary to put super() in constructor?
(6 answers)
Closed 1 year ago.
I don't really use super() anywhere but still the superclass' constructor is called. Why is it so?
class SuperDemo{
public static void main(String[] args) {
B subClass = new B();
subClass.showbiz();
}
}
class A{
int i;
A(){
i = 10;
}
}
class B extends A{
void showbiz(){
System.out.println("i in subclass " + i);
}
}
That's how the language works. If you don't explicitly call the superclass' constructor using super, the default constructor of the superclass will be called implicitly.

Why does Java base class constructor call derived class' methods? [duplicate]

This question already has answers here:
Polymorphism and Constructors
(2 answers)
Closed 3 years ago.
I have a derived Java class override a base class' method. When the base class calls the method, it executes the derived class' function, rather than its own. Why?
public class HelloWorld{
public static void main(String []args){
Derived d = new Derived();
System.out.println("Main");
}
}
class Base {
void f() {
System.out.println("Base::f()");
}
public Base() {
f();
}
}
class Derived extends Base {
void f() {
System.out.println("Derived::f()");
}
public Derived() {
f();
}
}
The code prints Derived::f() twice, I expect it to print Base::f() followed by Derived::f(), as would happen in C++
In Java, unlike in C++, instance methods are virtual by default. It means that a method call is dispatched at the run time according to the actual run-time class of an object (not at the compile time). In C++, you achieve this behaviour with keyword virtual.
Your Derived f() method is overriding the Base f() method. If you want to see the Base method try putting super.f(); in the start of the Derived f() method, this will call the super class' f() method.

If constructor is not inherited in java then why it parent class constructor is called in subclass by default? [duplicate]

This question already has answers here:
Does a subclass inherit constructors from it super class?
(6 answers)
Java Constructor Inheritance
(10 answers)
Closed 5 years ago.
Lets say we have two classes:
public class A{
public A(){
System.out.println("A");
}
}
and its subclasas
public class B extends A{
public B(){
System.out.println("B");
}
}
The output below would be
A
A
B
public static main(String[] args){
A a = new A();
B b = new B();
}
WHY like this is constructor is not inherited?
SHould not we call super() in subclass constructor to call constructor of parent?
thanks
when you create object of child class then super() call automatic by jvm for non-parameterized constructor.
and if your constructor is parameterized then you have to call by passing parameters like super(parameters).

Instance variable access by using Inheritance concept [duplicate]

This question already has answers here:
Overriding member variables in Java ( Variable Hiding)
(13 answers)
Closed 7 years ago.
I am using a Inheritance concept here. I have extended class A(Superclass) into class B(Subclass). And I have created an object of a subclass and by using that object I have called add() method. But it is printing a value 5(Super Class).
Why it didn't take subclass's value which is 10 ?
class A{
int a=5;
public void add(){
System.out.println(a);
}
}
class B extends A{
int a=10;
}
public class InheritExample {
public static void main(String args[]){
B b1=new B();
b1.add();
}
}
Help Appreciated.
Thanks.
There's no overriding of instance variables. Overriding only works on instance methods. Therefore, the method add of class A has access only to the a variable declared in A, even if you call that method for an instance of class B.

Is calling super's constructor redundant in this case? [duplicate]

This question already has answers here:
Is it unnecessary to put super() in constructor?
(6 answers)
Closed 8 years ago.
I always thought that when creating an object with a sub-class, we need to explicitly use super(arguments list) to call the constructor of the super class. However I did an experiment and realize that even without using the super(), the super class's constructor will be called automatically. Is this true?
If this is true, when is super() redundant and when it is not?
class Parent
{
public Parent()
{
System.out.println("Super Class");
}
}
class Child extends Parent
{
public Child()
{
super(); //Is this redundant?
System.out.println("Sub Class");
}
}
public class TestClass
{
public static void main(String[] args)
{
new Child();
}
}
OUTPUT (With super(); in Child Class):
Super Class
Sub Class
OUTPUT (Without super(); in Child Class):
Super Class
Sub Class
By default super() is added in all sub-class so don't need to call it explicitly.
The default behavior can be overridden by calling overloaded constructor of super class using super(args) or overloaded constructor of same class using this(args).
Suppose super class doesn't have no-argument constructor and you have created other constructor, in that case you have to call super(args) explicitly to resolve compile time error.
When in doubt, always consult the specification:
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.

Categories

Resources