Jak wywołać sparametryzowany konstruktor klasy nadrzędnej w dziedzictwie
/*How to call parameterized constructor of a Parent class in Inheritance*/
/*Parameterized constructor and Non Parameterized constructor*/
class Parent {
// Default constructor , not taking args
Parent() {
System.out.println("Non Parameterized constructor of Parent");
}
// Parameterized constructor, taking args
Parent(int x) {
System.out.println("constructor with parameter of Parent " + x);
}
}
/* Child class which extend from Parent class */
class Child extends Parent {
// Default constructor
Child() {
System.out.println("Non Parameterized constructor of Child");
}
Child(int y) {
System.out.println("constructor with parameter of Child");
}
Child(int x, int y) {// One is parameter of Super class int x,
/*
* -> * super is keyword that refer to super class and when you passing any
* parameter
* that means you are calling to constructor
*/
super(x);
System.out.println("2 parameters of Child " + y);
}
}
public class sample {
public static void main(String[] args) {
// Ctreat an obj of child class
Child child = new Child(10, 20);
}
}
Rajput