Menu
Topics Index
...
`

Class inside other class2957


If you need explanation Read this topic

If you need Answer Take test on this topic

Class inside other class2957
What will be the output of the following program?
public class Hyderabad1 {
    public static void main(String args[])
    {
            ChandaNagar obj1 = new ChandaNagar();
            System.out.print(obj1.operation(2, 3, 4));
            Hyderabad obj2 = new Hyderabad();
            System.out.print(" "+obj2.operation(2, 3, 4));
            System.out.print(" "+obj1.operation(2, 3));
            System.out.print(" "+obj1.operation(2, 3, 4, 5));
        
    }
}
class Hyderabad
{
    public int operation(int x, int y)
    {
        return x + y;
    }
    public  int operation(int x, int y, int z)
    {
        return x + y + z;
    }
    
}
class ChandaNagar extends Hyderabad
{
    public  int operation(int x, int y, int z)
    {
        return x * y * z;
    }
    public  int operation(int w, int x, int y, int z)
    {
        return w + x + y + z;
    }
}

A. 9 9 5 14
B. 24 9 5 14
C. Compilation Error
D. Runtime Error
Topic: Method Overloading Vs Method Overriding

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 B..here we have the Hyd and Hyd1 and Chandanagar class..

Here we have the concept of Inheritance...Child will get the properties of the its parent class using the extend keyword..
here Chandanagar class extending the Hyd class..so 2 methods in the Hyd class will inherited into the Chandanagar class...and here Overiding will occur which  means re defining the methods of Parent class in Child class..here return type must sam..except if the return type is non-priitive...

here we have overloading concept also..which means same method name but differ in arg type,num,order..

Coming to the program here we create the object for the Chandanagar class and here we call the operation() by passing the 2,3,4..so method in Chandanagar will get invoked and we are returning 2*3*4=24..

next we are creating the Instance for the Hyd class and invoking the operation() with 2,3,4 so method in Hyd will invoked Bcz calling object is type oof Hyd...so here here adding these 3 num which is 9...
next we are calling operation() by passing 2,3 with Chandanagr object..so inherited method will get executed...so returns 5...

next calling same method with 4 args...so it will invoke method in operation class...returns sum as 14

Posted by Uday Kumar    2014-12-03 12:47:51


Answer is B...obj1.operation(2,3,4) invokes operation method with 3 args in chandanagar class (using concept method overriding)....obj2.operation(2,3,4) calls operation method with 3 args from Hyderabad class....obj1.operation(2,3) calls operation method with 2 parameters in Hyderabad class (since that method is overloaded method and is inherited to chandanagr class).......obj1.operation(2,3,4,5) invokes operation method operation method with 4 parameters in Chandanagar class

Posted by Shashanka Mogaliraju    2014-12-03 12:47:56


ans b

method overloading

With the Java language, you can overload methods. Overloading is the practice of supplying more than one definition for a given method name in the same class. The compiler will automatically select the most appropriate one based on the arguments with which it is called.

Several restrictions govern an acceptable set of overloaded methods:

--Any two methods in a set of overloaded functions must have different argument lists (The name of the parameter is irrelevant. The type and order of the parameters is what's important).
--A difference in return type only is not sufficient to constitute an overload and is illegal.
Method overloading is generally used where a class can perform the same operation on more than one type of data.

method overriding

A sub class inherits methods from a super class. Sometimes, the sub class may want to provide a new version of methods defined in the super class. This is referred to as method overriding. Method overriding allows a sub class to provide its own implementation of a method already provided by one of its super classes.

A sub class method is provided with the same signature and return type as a method that is declared in its super class, the subclass is said to override that method. You can also override a method with the same signature that returns a subclass of the object returned by the original method. This facility (introduced in 5.0) is called covariant return type.

The subclass method does not inherit the functionality of its super class method unless it explicitly calls the super class method using the "super" keyword.

Several restrictions govern any particular method to override another correctly:

--The return type and method signature must be identical to those of a method in the super class.
--The accessibility must not be more restrictive than original method.
--The overriding method must not throw checked exceptions of classes that are not possible for the original method.

coming to this program we used both concepts.
here chandaNagar class inherits all the features of hyderabad class.
in main class chandaNagar and hyderabad objects created.by using that reference operation method gets called with diff num of parameters.

1st obj1.operation(2,3,4)
in chandaNagar class itself hve correct definition so funtion returns 2*3*4=24

2nd obj2.operation(2,3,4)
in Hydrabad class function returns 2+3+4=9

3rd obj1.operation(2,3)
chandaNagar extends hydrabad so it can use all the features of superclass.
return 2+3=5

4th obj1.operation(2,3,4,5)
returns 2+3+4+5=14

The compiler will automatically select the most appropriate one based on the arguments.

Posted by Maheshwari Natarajan    2014-12-03 13:57:14


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Damodar Ukkadala. 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    2014-12-05 05:03:34


© meritcampus 2019

All Rights Reserved.

Open In App