Q:

Java find output programs (Overriding) | set 3

0

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();
    }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Question 1:

Output:

/MethodOver.java:3: error: missing method body, or declare abstract
    public void fun();
                ^
1 error

Explanation:

The above program will generate syntax error because we cannot create an object of abstract class in Java.

Answer Question 2:

Output:

Derived.fun1() called

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:

Base.fun2() called

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:

/MethodOver.java:4: error: abstract methods cannot have a body
    abstract void fun2()
                  ^
1 error

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:

Derived.fun() called

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now