ADVERTISEMENT
ADVERTISEMENT

Java find output programs (Overriding) | set 3

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.
Submitted by Nidhi, on February 04, 2021

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

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.

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

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.

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

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.

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

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.

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

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.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.