When an object is created from an inherited class, the constructor of the superclass is performed before the constructor of the subclass. That is, when an object is created from an inherited class, the implicit constructor of the superclass is performed. In the case of overloading, if you want to call a constructor other than the implicit constructor, make an explicit call using super, the reserved word.

Implicit constructors are constructors that have no parameters. The sentence using super must be on the first line.

class A1 {
	double d1;
	A1() {
		System.out.println("Class A1 performs this constructor");
		d1 = 10 * 10;
	}
}

class A2 extends A1 {
	double d2;
	A2() {
		System.out.println("Perform a constructor for class A2");
		d2 = 10 * 10 * 10;
	}
}

class A3 extends A2 {
	double d3;
	A3() {
		System.out.println("Perform a constructor for class A3");
		d3 = 10 * 10 * 10 * 10;
	}
}

class Constructors1 {
	public static void main(String args[]) {
		A3 super1 = new A3();
		System.out.println("2 to the power of 10: " + super1.d1);
		System.out.println("3 to the power of 10: " + super1.d2);
		System.out.println("4 to the power of 10: " + super1.d3);
	}
}
Creating an object of an inherited class Example 1
class A1 {
	int d1;
	int s;
	A1(int S1) {
		System.out.println("Perform a constructor for class A1");
		s = s1;
		d1 = s * s;
	}
}

class A2 extends A1 {
	int d2;
	int t;
	A2(int s1, int t1) {
		super(s1); //Explicitly calling the constructor of the superclass
		System.out.println("Perform a constructor for class A2");
		t = t1;
		d2 = t * t;
	}
}

class Constructors2 {
	public static void main(String args[]) {
		A2 super2 = new A2(10, 20);
		System.out.println("The square of 10: " + super2.d1);
		System.out.println("The square of 20: " + super2.d2);
	}
}
Creating an object of an inherited class Example 2

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Post

  • Mastering the daily conversation in 4 languagesBasic sentences expressing my mood – I’m
  • 8 things you need to think about for work-life balance
    View on Youtube Image: juliezhuo.com who works as a design VP for Facebook, presented 8 ways for work and life balance in her article “The
  • 10 special abilities of Smartphone
    View on Youtube No matter where you go or what you do, your smartphone is always with us. We use the internet with a smartphone,
  • Java: abstract class, abstract method
    An abstract class in Java is a class that declares an abstract function to be implemented in a subclass as an abstract method. An abstract
  • Java: inheritance and constructors
    When an object is created from an inherited class, the constructor of the superclass is performed before the constructor of the subclass. That is, when
  • Java reserved words: super, final
    super is a Java reserved word that is used to access member variables or methods of a superclass that is obscured by a subclass. Access
  • Java: finalize method and garbage collection
    The Java Virtual Machine(JVM) automatically performs garbage collection for objects that are no longer needed for efficient system operation. At this point, the JVM calls
  • Java: Method overloading
    Method overloading is the same concept as constructor overloading. That is, you can use methods of the same name in the same class, but methods
  • Java final variable
    You can specify an end variable using the reserved word final. The final variable represents a constant value that can not be changed. Typically, final
  • Java class variables
    Class variables are declared with static and have the concept of global variables. Class variables are used for the following purposes. Object references and object