Q:

Java find output programs (Overriding) | set 1

belongs to collection: Java find output programs

0

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.

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

  }
}

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

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

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

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

All Answers

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

Answer Question 1:

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.

Answer Question 2:

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.

Answer Question 3:

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

Answer Question 4:

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.

Answer Question 5:

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.

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

total answers (1)

Java find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java find output programs (Overriding) | set 2... >>
<< Java find output programs (Overloading) | set 2...