Java Programming - Declarations and Access Control - Discussion

10. 

What will be the output of the program?

class Super 
{ 
    public Integer getLength() 
    {
        return new Integer(4); 
    } 
} 

public class Sub extends Super 
{ 
    public Long getLength() 
    {
        return new Long(5); 
    } 

    public static void main(String[] args) 
    { 
        Super sooper = new Super(); 
        Sub sub = new Sub(); 
        System.out.println( 
        sooper.getLength().toString() + "," + sub.getLength().toString() ); 
    } 
}

[A]. 4, 4
[B]. 4, 5
[C]. 5, 4
[D]. Compilation fails.

Answer: Option D

Explanation:

Option D is correct, compilation fails - The return type of getLength( ) in the super class is an object of reference type Integer and the return type in the sub class is an object of reference type Long. In other words, it is not an override because of the change in the return type and it is also not an overload because the argument list has not changed.


Manjunath said: (Oct 12, 2011)  
Your explanation is not exactly correct...Correct reason is,
2 methods getLength() can have same name, argument & also return type. but return type should be compatable. here return type Long in sub class is not compatable with Integer of super class.
so to compile change the 2 return types objects from super and sub.

Datla Kranthi said: (May 5, 2014)  
class Super
{
public Integer getLength()
{
return new Integer(4);
}
}

public class Sub extends Super
{
public Integer getLength()
{
return new Integer(5);
}

public static void main(String[] args)
{
Super sooper = new Super();
Sub sub = new Sub();
System.out.println(
sooper.getLength().toString() + "," + sub.getLength().toString() );
}
}


Friends, if we keep Integer before getlength then we get result as 4,5.

Pratik said: (Sep 6, 2015)  
Return type is does not matter in case of overriding.

Farid said: (Feb 17, 2016)  
Answer [A].

Because overriding is not correct so when select getLength() it is select superclass method.

Bhavyasree said: (Apr 6, 2016)  
After Java5 version if base class method return type is nonprimitive we can change the return of overridden method in subclass this is called Covariant return type in java.

Krishna said: (Jun 1, 2017)  
Super is a reserved key word in java. How can we create a class by its name?

Kkhushi said: (Jul 18, 2017)  
The super is reserved but not Super difference in capitals.

Khagendra said: (Nov 27, 2017)  
@Krishna.

Java is a case sensitive programming language.

Khagendra said: (Nov 27, 2017)  
Overloading can't possible because it happens within a class, but overriding is happen in two different class in inheritance and here the two function name getLength () return different value. So, it is a contradiction of overriding concept, that's why the compile-time error occurs.

Gopi said: (Dec 28, 2017)  
Is Integer valid return type. I have used the only int all the time.

Nitin said: (Feb 29, 2020)  
Why its necessary to override super class method, can't we treat them different method and access it by class instance.

Monika said: (Apr 27, 2021)  
@Nitin.

It is possible. But in this case method name same. So overloading is also not possible.

Nikhil said: (Jul 18, 2021)  
In java, method overloading is not possible by changing the return type of the method only because of ambiguity.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.