Java find output programs (Overriding) | set 1

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

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

  }
}

Output:

/MethodOver.java:7: error: '{' expected
class Derived: Base {
             ^
1 error

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.

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

Output:

Base.fun() called
Derived.fun() called

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.

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

Output:

/MethodOver.java:8: error: <identifier> expected
  public override void fun() {
                 ^
1 error

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:

@Override
public  void fun()
{
       System.out.println("Derived.fun() called");
}

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

Output:

Base.fun() called
Derived.fun() called

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.

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

Output:

/MethodOver.java:3: error: <identifier> expected
    public virtual void fun()
                  ^
1 error

Explanation:

The above program will generate syntax error because virtual, here we did not require virtual to override the fun() method.





Comments and Discussions!

Load comments ↻





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