Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nitin Jayant
committed
Mar 31, 2021
1 parent
f2d6fea
commit c391bc0
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class BaseClass{ | ||
int x = 10; | ||
BaseClass(){ | ||
System.out.println("Base Class Constructor"); | ||
} | ||
} | ||
|
||
class DerivedClass extends BaseClass{ | ||
int x = 20; | ||
DerivedClass(){ | ||
System.out.println("Derived Class Constructor"); | ||
} | ||
|
||
public void callSuper(){ | ||
System.out.println(super.x); // refers to the base class's x | ||
} | ||
} | ||
|
||
class BaseClass2{ | ||
BaseClass2(int x){ | ||
System.out.println("Base Class Constructor"); | ||
} | ||
} | ||
|
||
class DerivedClass2{ | ||
DerivedClass2(){ | ||
//super(10); | ||
System.out.println("Derived Class Constructor"); | ||
} | ||
} | ||
|
||
|
||
class Questions{ | ||
public void Q1(){ | ||
DerivedClass derivedClass = new DerivedClass(); // the base class constructor will also be called. | ||
|
||
derivedClass.callSuper(); | ||
} | ||
|
||
public void Q2(){ | ||
DerivedClass2 derivedClass = new DerivedClass2(); // the base class construct will not be called here because the default constructor is not present in the base class. | ||
// we have to call super and pass value to call the constructor of base class. | ||
} | ||
} | ||
|
||
public class OOPSQuestions{ | ||
public static void main(String[] args){ | ||
Questions questions=new Questions(); | ||
|
||
questions.Q2(); | ||
} | ||
} |