Find the output of Java programs | Overriding | Set 3: Enhance the knowledge of Java Overriding concepts by solving and finding the output of some Java programs.
Question 1:
abstract class Base
{
abstract void fun1();
void fun2()
{
System.out.println("Base.fun1() called");
}
}
class Derived extends Base
{
@Override
public void fun1()
{
System.out.println("Derived.fun1() called");
}
}
public class MethodOver
{
public static void main(String []args)
{
Base B = new Base();
B.fun2();
}
}
Question 2:
abstract class Base
{
abstract void fun1();
void fun2()
{
System.out.println("Base.fun2() called");
}
}
class Derived extends Base
{
@Override
public void fun1()
{
System.out.println("Derived.fun1() called");
}
}
public class MethodOver
{
public static void main(String []args)
{
Derived D = new Derived();
D.fun1();
}
}
Question 3:
abstract class Base
{
abstract void fun1();
void fun2()
{
System.out.println("Base.fun2() called");
}
}
class Derived extends Base
{
@Override
public void fun1()
{
System.out.println("Derived.fun1() called");
}
}
public class MethodOver
{
public static void main(String []args)
{
Derived D = new Derived();
D.fun2();
}
}
Question 4:
abstract class Base
{
abstract void fun1();
abstract void fun2()
{
System.out.println("Base.fun2() called");
}
}
class Derived extends Base
{
@Override
public void fun1()
{
System.out.println("Derived.fun1() called");
}
@Override
public void fun2()
{
System.out.println("Derived.fun2() called");
}
}
public class MethodOver
{
public static void main(String []args)
{
Base D = new Derived();
D.fun2();
}
}
Question 5:
abstract class Base
{
abstract void fun();
}
class Derived extends Base
{
@Override
public void fun()
{
System.out.println("Derived.fun() called");
}
}
public class MethodOver
{
public static void main(String []args)
{
new Derived().fun();
}
}
Answer Question 1:
Output:
Explanation:
The above program will generate syntax error because we cannot create an object of abstract class in Java.
Answer Question 2:
Output:
Explanation:
In the above program, we created an abstract class Base and a Derived class, the Base class contains an abstract method fun1() and non-abstract method fun2() and then we override the fun1() method in the Derived class.
Now look to the main() method,
Derived D = new Derived(); D.fun1();
Here, we created object D of Derived class and then called fun1() method, which will print "Derived.fun1() called" on the console screen.
Answer Question 3:
Output:
Explanation:
In the above program, we created an abstract class Base and a Derived class, the Base class contains an abstract method fun1() and non-abstract method fun2() and then we override the fun1() method in the Derived class.
Now look to the main() method,
Derived D = new Derived(); D.fun2();
Here, we created object D of Derived class and then called fun2() method, which will print "Derived.fun2() called" on the console screen.
Answer Question 4:
Output:
Explanation:
The above program will generate syntax errors because here we defined abstract method fun2() in Base class. But we cannot define the body of the abstract class in Java.
Answer Question 5:
Output:
Explanation:
In the above program, we created an abstract class Base and a Derived class, the Base class contains an abstract method fun() and then we override fun() method in the Derived class.
Now look to the main() method,
new Derived().fun();
Here, we created the anonymous object of the Derived class and then called the fun() method that will print "Derived.fun() called" on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer