Topics Index

Creating classes and method Overriding2951

If you need explanation Read this topic

If you need Answer Take test on this topic

Creating classes and method Overriding2951
What will be the output of the following program?
public class Outer {
    public static void main(String args[]) {
        Computer mouse = new Laptop();
        System.out.println(mouse.getValue(100, 200));
class NoteBook {
    int getValue(int a, int b) {
        if (a > b)
            return a;
            return b;
class Computer extends NoteBook {
    int getValue(int a, int b) {
        return a * b;
class Laptop extends Computer {
    int getValue(int a, int b) {
        return b - a;

A. 100
B. 20000
C. 200
D. Compilation Error
E. Runtime Error
Topic: Method Overriding In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is we have the method overriding..means re-defining the Parent class method in the child method overriding signature,parameters must be java Parent class reference can
refer child class Object...

NoteBook is Parent class and Computer is Child of NoteBook and  Laptop are Child class of
the Parent class we have the getvalue() and we are overriding these method in
Computer n Laptop class with different operation

in the main class we create the reference for the Computer class  and create the Object for the Child class(Laptop) we are calling the getValue() from main() which class getValue() get invoked..?? while running JVM lo execute the methods
in the Object type class means here  getValue() in Laptop get Invoked Bcz object is type of Laptop..and returns difference as 100

Posted by Uday Kumar    2014-12-26 10:58:35


--Here the concept of "METHOD OVERRIDING" is used.Method overriding means same method signature with different implementation defined in different classes.For method overriding "INHERITANCE" is required.

--In above example three classes are defined :

1) Notebook ---> parent class
2)Computer  --->child class of parent
3)Laptop    --->child class of Computer

"MULTILEVEL INHERITANCE" is used in above examle.

--Three of above classes contain the getValue() method with same signature.

--In Main() method :--->
Object of Laptop(Child class) is passed into the reference of Computer class(parent of Laptop).

--Whenever getvalue() is called with the object of computer class then getvalue() of Laptop class will get exexuted as it is holding the object of Laptop.

--getvalue(100,200) two values are passed into(int a,int b)
then b-a=200-100=100

100 will get executed.

Posted by Mânïshå Mùlchåndânï    2014-12-26 17:48:06

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar, Mânïshå Mùlchåndânï,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Gopinath Manchikanti. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-12-29 05:07:55

© meritcampus 2019

All Rights Reserved.

Open In App