4. class one
{ public void print_geek()
{
System.out.println("Geeks");
}
}
class two extends one
{
public void print_for()
{
System.out.println("for");
}
}
class three extends two
{
public void print_geek()
{
System.out.println("Geeks");
}
} // Drived class
public class Main
{ public static void main(String[] args)
{
three g = new three();
g.print_geek();
g.print_for();
g.print_geek(); G
}
}
OUTPUT Geeks for Geeks
Example Multilevel inheritance
5. class one
{
public void print_geek()
{
System.out.println("Geeks");
}
}
class two extends one
{
public void print_for()
{
System.out.println("for");
}}
class three extends one
{ /*............*/
}
// Drived class
public class Main
{ public static void main(String[] args)
{
three g = new three();
g.print_geek();
two t = new two();
t.print_for();
g.print_geek();
} }
OUTPUT GeeGeeks for Geeks
concept of Hierarchical inheritance
6. Which statements are true?
Select the two correct answers.
(a) A class can only be extended by one class.
(b) Every Java object has a public method named equals.
(c) Every Java object has a public method named length.
(d) A class can extend any number of classes.
(e) A non-final class can be extended by any number of classes.
Which statements are true?
(a) A subclass must define all the methods from the superclass.
(b) It is possible for a subclass to define a method with the same name and
parameters as a method defined by the superclass.
(c) It is possible for a subclass to define a field with the same name as a field
defined by the superclass.
(d) It is possible for two classes to be the superclass of each other.
MCQ
7. Java this keyword
this keyword automatically holds the reference to current instance of a
class. It is very useful in scenarios where we are inheriting a method from
parent class into child class, and want to invoke method from child class
specifically. We can use this keyword to access static fields in the class as
well, but recommended approach to access static fields using class
reference e.g. MyClass.STATIC_FIELD.
Java super keyword
Similar to this keyword, super also is a reserved keyword in Java. It always
hold the reference to parent class of any given class. Using super keyword,
we can access the fields and methods of parent class in any child class.
**Can we have this () and super () together?
Both this() and super() are constructor calls. Constructor call must always
be the first statement. So we can not have two statements as first
statement, hence either we can call super() or we can call this() from the
constructor, but not both.
Use of this and Super
8. class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal class
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
}}
Output black white
Predict the output??
9. Which statement is true?
Select the one correct answer.
(a) A super() or this() call must always be provided explicitly as the first
statement in the body of a constructor.
(b) If both a subclass and its superclass do not have any declared
constructors,the implicit default constructor of the subclass will call
super() when run.
(c) If neither super() nor this() is declared as the first statement in the body of
a constructor, this() will implicitly be inserted as the first statement.
(d) If super() is the first statement in the body of a constructor, this() can be
declared as the second statement.
(e) Calling super() as the first statement in the body of a constructor of a
subclass will always work, since all superclasses have a default constructor.
MCQ