Menu
Topics Index
...
`

Strings2048


If you need explanation Read this topic

If you need Answer Take test on this topic

Strings2048
What will be the output of following program?
class Merit {
    public Integer getLenght() {
        return new Integer(5);
    }
}
public class Campus extends Merit {
    public Long GetLenght() {
        return new Long(10);
    }
    public static void main(String[] args) {
        Merit merit = new Merit();
        Campus camp = new Campus();
        System.out.println(merit.GetLenght().toString() + "," + camp.GetLenght().toString());
    }
}

A. 5,10
B. 10,10
C. 5,5
D. 10,5
E. Compilation Error or Runtime Error
Topic: Conclusion (Exploring java.lang)

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.

a

Posted by Shubham Bansal    2015-01-30 13:39:11


Ans is a
Here class Integer is wraper class it do autoboxing añd uñboxing when needs.
Execution : in main object is created for merton and campus....
In the print statement method of merit class is called now Integer class inbox the value and pass to it now it converts to string it prints 5.

Same now campus class method called. By its object so it retuns the bale 10 and converts to string prints 10....

Ans is 5 10

Posted by Ananthoju Arun Chary    2015-01-30 15:15:29


ANS IS A (5,10)

--Here the concept of "method overriding" is used.

--Method overloading: It means defining the method with
exactly same signature in Base and Derived class.Where
the Derived class is defined by extending base class.

--Here,GetLength() method is overridden in Merit(base
class)and Campus (Derived class).GetLength() method of
merit returns Integer 5 whereas GetLength() of campus
returns Long 10.It means whenever object of derived class
i.e. Campus is created,GetLength() method of class campus
will be called as method of derived class overrides the
method of Merit class.

--In main  method(),object of class merit and campus is
created and getLength() method is called with merit and
campus class's object.

--toString() method is used to convert any data type to
String.

--Output :
5,10

Posted by Mânïshå Mùlchåndânï    2015-01-30 18:44:34


Ans is A..here we have the Inheritance concept where child class inherit the properties of the Parent class..
another concept Method Overriding where child class redefines the method in the Parent class with same name and sign and same parameters.
Execution:
here we have the merit class as Parent class with 1 method getLength() which is returning the Integer Object..and Campus class extending the Merit class and overriding the getLength() in the Child class by redefining it as it is returning the object of the Long class..in the main() we create the instance for the class Merit ans class Campus..in the Sop we are calling the getLength() with the merit object than method in the Merit is invoked and returns 5 and next we are calling the getLength() with the camp object so it will invoke the method in the Campus class and returns the 10 so op is A

Posted by Uday Kumar    2015-01-31 03:25:49


Ans is E...Compilation Error..We cannot call GetLenght() method by the object of Merit Class as it is super class it cannot access subclass methods.

Posted by Krishna Kanth    2015-01-31 10:05:50


This dose is now closed and the winners are Krishna Kanth,  for 'First Correct Comment', Krishna Kanth,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Shubham Bansal. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-02 06:43:45


As per the rules of merit campus edited comments are not allowed but u hav given correct and best comment to one whose comment is edited. Merit Campus

Posted by Mânïshå Mùlchåndânï    2015-02-03 15:11:23


© meritcampus 2019

All Rights Reserved.

Open In App