Podcast
Questions and Answers
What is the relationship between the Person class and the Employee class in the example code?
What is the relationship between the Person class and the Employee class in the example code?
What is the term for when one class inherits multiple classes?
What is the term for when one class inherits multiple classes?
What is the advantage of using inheritance in programming?
What is the advantage of using inheritance in programming?
What is the term for the class that is being inherited from?
What is the term for the class that is being inherited from?
Signup and view all the answers
What is the relationship between the Shape class and the Circle, Triangle, and Rectangle classes in the example hierarchy?
What is the relationship between the Shape class and the Circle, Triangle, and Rectangle classes in the example hierarchy?
Signup and view all the answers
Why is multiple inheritance not supported in Java?
Why is multiple inheritance not supported in Java?
Signup and view all the answers
What is the advantage of using inheritance in the example code?
What is the advantage of using inheritance in the example code?
Signup and view all the answers
What is the term for the Employee class in the example code?
What is the term for the Employee class in the example code?
Signup and view all the answers
What is the purpose of the this
keyword in the constructor Student(int rollno,String name,String course)
?
What is the purpose of the this
keyword in the constructor Student(int rollno,String name,String course)
?
Signup and view all the answers
What will happen if you call this.fee=fee;
before this(rollno,name,course);
in the constructor Student(int rollno,String name,String course,float fee)
?
What will happen if you call this.fee=fee;
before this(rollno,name,course);
in the constructor Student(int rollno,String name,String course,float fee)
?
Signup and view all the answers
What is the purpose of the super
keyword in Java?
What is the purpose of the super
keyword in Java?
Signup and view all the answers
What will happen if you try to call a private method of the super class using the super
keyword?
What will happen if you try to call a private method of the super class using the super
keyword?
Signup and view all the answers
What is the purpose of the this
keyword in the method m(S2 obj)
?
What is the purpose of the this
keyword in the method m(S2 obj)
?
Signup and view all the answers
What will happen if you call this.m()
in the method n()
?
What will happen if you call this.m()
in the method n()
?
Signup and view all the answers
What is the purpose of the this
keyword in the constructor A(int x)
?
What is the purpose of the this
keyword in the constructor A(int x)
?
Signup and view all the answers
What will happen if you call this()
in the default constructor A()
?
What will happen if you call this()
in the default constructor A()
?
Signup and view all the answers
What is the purpose of the this
keyword in the method getA()
?
What is the purpose of the this
keyword in the method getA()
?
Signup and view all the answers
What will happen if you try to call this()
in the method display()
?
What will happen if you try to call this()
in the method display()
?
Signup and view all the answers
What is the purpose of the super keyword in Java?
What is the purpose of the super keyword in Java?
Signup and view all the answers
What is the output of the following code: class ParentClass { public ParentClass() { System.out.println("Parent Class default Constructor"); } } public class SubClass extends ParentClass { public SubClass() { super(); System.out.println("Child Class default Constructor"); } public static void main(String args[]) { SubClass s = new SubClass(); } }
What is the output of the following code: class ParentClass { public ParentClass() { System.out.println("Parent Class default Constructor"); } } public class SubClass extends ParentClass { public SubClass() { super(); System.out.println("Child Class default Constructor"); } public static void main(String args[]) { SubClass s = new SubClass(); } }
Signup and view all the answers
What is the purpose of the this keyword in Java?
What is the purpose of the this keyword in Java?
Signup and view all the answers
What is method overriding in Java?
What is method overriding in Java?
Signup and view all the answers
What is the output of the following code: class ParentClass { public void disp() { System.out.println("Parent Class method"); } } public class SubClass extends ParentClass { public void disp() { System.out.println("Child Class method"); } public void show() { super.disp(); } public static void main(String args[]) { SubClass s = new SubClass(); s.show(); } }
What is the output of the following code: class ParentClass { public void disp() { System.out.println("Parent Class method"); } } public class SubClass extends ParentClass { public void disp() { System.out.println("Child Class method"); } public void show() { super.disp(); } public static void main(String args[]) { SubClass s = new SubClass(); s.show(); } }
Signup and view all the answers
What is the difference between a default constructor and a parameterized constructor in Java?
What is the difference between a default constructor and a parameterized constructor in Java?
Signup and view all the answers
What is the purpose of constructor chaining in Java?
What is the purpose of constructor chaining in Java?
Signup and view all the answers
What is the output of the following code: class ParentClass { int val = 999; } public class SubClass extends ParentClass { int val = 123; public void disp() { System.out.println("Value is : " + super.val); } public static void main(String args[]) { SubClass s = new SubClass(); s.disp(); } }
What is the output of the following code: class ParentClass { int val = 999; } public class SubClass extends ParentClass { int val = 123; public void disp() { System.out.println("Value is : " + super.val); } public static void main(String args[]) { SubClass s = new SubClass(); s.disp(); } }
Signup and view all the answers
What is inheritance in Java?
What is inheritance in Java?
Signup and view all the answers
What is the output of the following code: class ParentClass { public void disp() { System.out.println("Parent Class method"); } } public class SubClass extends ParentClass { public void show() { disp(); } public static void main(String args[]) { SubClass s = new SubClass(); s.show(); } }
What is the output of the following code: class ParentClass { public void disp() { System.out.println("Parent Class method"); } } public class SubClass extends ParentClass { public void show() { disp(); } public static void main(String args[]) { SubClass s = new SubClass(); s.show(); } }
Signup and view all the answers
Study Notes
Inheritance and super
Keyword
- Inheritance allows for code reuse and more intuitive/expressive code.
-
super
keyword can be used to:- Call the constructor of the parent class (
super()
). - Refers to a variable in the parent class (
super.variable_name
). - Refers to a method of the parent class (
super.method_name
).
- Call the constructor of the parent class (
Examples of super
Keyword
-
super()
invokes the constructor of the parent class:
class ParentClass {
public ParentClass() {
System.out.println("Parent Class default Constructor");
}
}
class SubClass extends ParentClass {
public SubClass() {
super(); // calls ParentClass constructor
System.out.println("Child Class default Constructor");
}
}
-
super.variable_name
refers to a variable in the parent class:
class ParentClass {
int val = 999;
}
class SubClass extends ParentClass {
int val = 123;
public void disp() {
System.out.println("Value is : " + super.val); // prints 999
}
}
-
super.method_name
refers to a method of the parent class:
class ParentClass {
public void disp() {
System.out.println("Parent Class method");
}
}
class SubClass extends ParentClass {
public void disp() {
System.out.println("Child Class method");
}
public void show() {
super.disp(); // calls ParentClass method
}
}
Review of Inheritance
- Inheritance allows for code reuse and more intuitive/expressive code.
- A child class can inherit from a parent class using the
extends
keyword. - A parent class can have multiple child classes.
Multiple Inheritance
- Not supported in Java through classes.
- A class cannot extend more than one class.
this
Keyword
-
this
keyword can be used to:- Invoke current class method.
- Invoke current class constructor.
- Pass as an argument in the method.
- Pass as an argument in the constructor call.
- Return current class instance.
Examples of this
Keyword
-
this
to invoke current class method:
class A {
void m() {
System.out.println("hello m");
}
void n() {
this.m(); // same as m()
}
}
-
this
to invoke current class constructor:
class A {
A() {
System.out.println("hello a");
}
A(int x) {
this(); // calls default constructor
System.out.println(x);
}
}
-
this
to pass as an argument in the method:
class S2 {
void m(S2 obj) {
System.out.println("method is invoked");
}
void p() {
m(this);
}
}
-
this
to pass as an argument in the constructor call:
class B {
A4 obj;
B(A4 obj) {
this.obj = obj;
}
void display() {
System.out.println(obj.data);
}
}
class A4 {
int data = 10;
A4() {
B b = new B(this);
b.display();
}
}
-
this
to return current class instance:
class A {
A getA() {
return this;
}
void msg() {
System.out.println("Hello java");
}
}
Note: The above examples are just a few illustrations of how this
and super
keywords can be used in Java.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
This quiz is about the 'super' keyword in Java, its usage, and its role in inheritance. It covers how to invoke the parent class constructor, access variables, and call methods.