Java find output programs (Overriding) | set 2

Find the output of Java programs | Overriding | Set 2: Enhance the knowledge of Java Overriding concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on February 04, 2021

Question 1:

class Base
{
    public 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)
    {
        Base B = new Base();

        B.fun();
    }
}

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 here we did not define the method body in the non-abstract class.

Question 2:

class Base
{
    final 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();
    }
}

Output:

/MethodOver.java:12: error: fun() in Derived cannot override fun() in Base
    public void fun()
                ^
  overridden method is final
1 error

Explanation:

The above program will generate syntax error because we here define the final fun() method in Base class and override into Derived class. But we cannot override the final method in java.

Question 3:

class Base
{
    void fun()
    {
        int i=1;
        
        XYZ:
            if(i<=5)
            {
                System.out.println(i*i)
                i++;
                goto XYZ;
            }
    }
}

class Derived extends Base
{
    @Override
    public void fun()
    {
        int i=1;
        int n=5;
        
        XYZ:
            if(i<=10)
            {
                System.out.println(n*i)
                i++;
                goto XYZ;
            }
    }
}

public class MethodOver
{
    public static void main(String []args)
    {
        Base B = new Base();

        B.fun();

        B = new Derived();
        B.fun();
    }
}

Output:

/MethodOver.java:10: error: ';' expected
                System.out.println(i*i)
                                       ^
/MethodOver.java:12: error: illegal start of expression
                goto XYZ;
                ^
/MethodOver.java:12: error: not a statement
                goto XYZ;
                     ^
/MethodOver.java:28: error: ';' expected
                System.out.println(n*i)
                                       ^
/MethodOver.java:30: error: illegal start of expression
                goto XYZ;
                ^
/MethodOver.java:30: error: not a statement
                goto XYZ;
                     ^
6 errors

Explanation:

The above program will generate syntax errors because we cannot use the goto statement in Java.

Question 4:

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)
    {
        Base B = new Derived();
        B.fun();
    }
}

Output:

/MethodOver.java:1: error: Base is not abstract and does not override 
abstract method fun() in Base
class Base
^
1 error

Explanation:

The above program will generate syntax error because we cannot declare an abstract method in a non-abstract class in java.

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)
    {
        Base B = new Derived();
        B.fun();
    }
}

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,

Base B = new Derived();
B.fun();

Here, we created object B using Derived class constructor and then called fun() method, here fun() method of Derived class will be called and print "Derived.fun() called" on the console screen.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.