[!INDEX]
- Def. - allow subclass to inherit , ct() pt()
- single inheritance - father money son job
- multilevel inheritance-grandfather- father- son
- hierarchy Mother - daughter - son
- multiple inheritance , same son inherit two father money mt()
- child subclass implicitly call parent class NO ARGUMENT ct()
- child subclass explicitly call parent class ARGUMENT ct().+super
- Rules for parent ct
1. Def. - allow subclass to inherit , ct() pt()
[!NOTE]
- Allows a new class (subclass or derived class) to inherit the properties and methods of an existing class (superclass or base class). This promotes code reuse and establishes a hierarchical relationship between classes.
- constructor and private method can not inherit
2. single inheritance - father money son job
public class Father {
void money() { // Method in class E05
System.out.println("Lot of Money");
}
}
class Son extends Father { // B2 is a subclass of E05
void job() { // Method in class B2
System.out.println("Very less Money");
}
public static void main(String[] args) {
Father fatherObj = new Father();
fatherObj.money();
fatherObj.job();
Son sonObj = new Son();
sonObj.money();
sonObj.job();
}
}
3. multilevel inheritance-grandfather- father- son
class Grandfather {
void money() {
System.out.println("lot of money");
}
}
class Father extends Grandfather {
void job() {
System.out.println("less money");
}
}
public class Son extends Father {
void pocketMoney() {
System.out.println("broke");
}
public static void main(String[] args) {
Grandfather grandfatherObj = new Grandfather();
System.out.println("\n - - - - - - - - Printing = ob1 ");
grandfatherObj.money();
grandfatherObj.job(); // Compile-time error: showB is not a method of E06
grandfatherObj.pocketMoney(); // Compile-time error: showC is not a method of E06
Father fatherObj = new Father();
System.out.println(" - - - - - - - - Printing = ob2 ");
fatherObj.money();
fatherObj.job();
fatherObj.pocketMoney(); // Compile-time error: showC is not a method of B3
Son sonObj = new Son();
System.out.println(" - - - - - - - - Printing = ob3 ");
sonObj.money();
sonObj.job();
sonObj.pocketMoney();
}
}
4. hierarchy Mother - daughter - son
class Mother {
void momLove() {
System.out.println("mom love children");
}
}
class Daughter extends Mother {
void doDance() {
System.out.println("she dances");
}
}
public class Son extends Mother {
void rideBike() {
System.out.println("he rides bike");
}
public static void main(String[] args) {
Mother momObj = new Mother();
momObj.momLove(); // Calls showA() method from E07
Daughter daughterObj = new Daughter();
daughterObj.momLove(); // Calls inherited showA() method from E07
daughterObj.doDance(); // Calls showB() method from B4
Son sonObj = new Son();
sonObj.momLove(); // Calls inherited showA() method from E07
sonObj.rideBike(); // Calls showC() method from C4
}
}
5. multiple inheritance , same son inherit two father money mt()
/*07, Multiple i/h with 2 clS and same m/t. */
class fatherOfRaj {
void money() {
System.out.println("lot of money");
}
}
class fatherOfManoj {
void money() {
System.out.println("lot of money");
}
}
public class Son extends fatherOfRaj, fatherOfManoj { // let assume
public static void main(String[] args) {
fatherOfRaj rajObj = new fatherOfRaj();
rajObj.money(); // complier confused which method is this
fatherOfManoj manojObj = new fatherOfManoj();
manojObj.money();
}
}
// E08.java:12: error: '{' expected class C5 extends E08, B5{
6. child subclass implicitly call parent class NO ARGUMENT ct()
class Parent {
// No-argument constructor
Parent() {
System.out.println("Parent no-argument constructor called");
}
}
class Child extends Parent {
Child() {
// super() is called implicitly by the compiler, calling paent class ct
System.out.println("Child constructor called");
}
public static void main(String[] args) {
Child child = new Child();
}
}
7. child subclass explicitly call parent class ARGUMENT ct().+ super
class Parent {
Parent(int x) {
System.out.println("Parent no-argument constructor called");
}
Parent() {
System.out.println("constructor called without parameters");
}
}
class Child extends Parent {
Child() {
super(4); // only one called
System.out.println("Child constructor called");
}
public static void main(String[] args) {
Child child = new Child();
}
}
8. rules for parent ct
[!NOTE]
- child.java:13: error: call to super must be first statement in constructor
- When a new object is created, the parent class's constructor needs to be executed first to ensure that the inherited fields are initialized correctly.
- If you don’t explicitly call a parent constructor, Java implicitly adds a call to
super()(the no-argument constructor of the parent class) as the first statement.