Find the output of Java programs | Overriding | Set 1: Enhance the knowledge of Java Overriding concepts by solving and finding the output of some Java programs.
Question 1:
class Base {
public void fun() {
System.out.println("Base.fun() called");
}
}
class Derived: Base {
public void fun() {
System.out.println("Derived.fun() called");
}
}
public class MethodOver {
public static void main(String[] args) {
Base B = new Base();
B.fun();
B = new Derived();
B.fun();
}
}
Question 2:
class Base {
public void fun() {
System.out.println("Base.fun() called");
}
}
class Derived extends Base {
public void fun() {
System.out.println("Derived.fun() called");
}
}
public class MethodOver {
public static void main(String[] args) {
Base B = new Base();
B.fun();
B = new Derived();
B.fun();
}
}
Question 3:
class Base {
public void fun() {
System.out.println("Base.fun() called");
}
}
class Derived extends Base {
public override void fun() {
System.out.println("Derived.fun() called");
}
}
public class MethodOver {
public static void main(String[] args) {
Base B = new Base();
B.fun();
B = new Derived();
B.fun();
}
}
Question 4:
class Base
{
public void fun()
{
System.out.println("Base.fun() called");
}
}
class Derived extends Base
{
@Override
public void fun()
{
System.out.println("Derived.fun() called");
}
}
public class MethodOver
{
public static void main(String []args)
{
Base B = new Base();
B.fun();
B = new Derived();
B.fun();
}
}
Question 5:
class Base
{
public virtual void fun()
{
System.out.println("Base.fun() called");
}
}
class Derived extends Base
{
@Override
public void fun()
{
System.out.println("Derived.fun() called");
}
}
public class MethodOver
{
public static void main(String []args)
{
Base B = new Base();
B.fun();
B = new Derived();
B.fun();
}
}
Answer Question 1:
Output:
Explanation:
The above program will generate syntax error because here we used colon ":" operator to inherit Base class into Derived class. We need to use the extends keyword for inheritance in Java.
Answer Question 2:
Output:
Explanation:
In the above program, we created three classes Base, Derived, and MethodOver. Here, Base class inherited in Derived class. Here, both Base and Derived class contain the fun() method.
Now look to the main() method of MethodOver class - Here, we created the object B of Base class and then called fun() method, then it will call the fun() method of Base class.
B = new Derived();
Using the above statement we re-initialized object B using Derived class. Then we called the fun() method, then it will call the fun() method of Base class again. If we want to override the method in the derives class then we need to use the Override modifier.
Answer Question 3:
Output:
Explanation:
The above program will generate syntax errors because we used incorrect syntax to override the fun() method in Java.
The correct way is given below:
Answer Question 4:
Output:
Explanation:
In the above program, we created 3 classes Base, Derived, and MethodOver. Here, we override the fun() method of Base class in the Derived class.
Now look to the main() method: Here, we created the object B of Base class and then called the fun() method of Base class. Then we re-initialized object B using the constructor of Derived class and then called fun() method, here fun() method of Derived will call and print "Derived.fun() called" on the console screen.
Answer Question 5:
Output:
Explanation:
The above program will generate syntax error because virtual, here we did not require virtual to override the fun() method.
need an explanation for this answer? contact us directly to get an explanation for this answer