Skip to content

Commit

Permalink
oops added
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitin Jayant committed Mar 31, 2021
1 parent f2d6fea commit c391bc0
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions OOPSQuestions.java
@@ -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();
}
}

0 comments on commit c391bc0

Please sign in to comment.