• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Design single method with different input type and different data type

 
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have Two method both are do the same work except the data type of input parameter and data type of return parameter.

If I use Overloading then in each method I have to place the same code, it creates code redundancy.

My expectation is I wanted to have a class which accepts different data type data as input and return data based on input dat.

How I can be implemented in OOPs?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overloading . and if you give different number of parameter then it make sense. but use overloading judiciously because it bit confuse
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd have to create a class or interface from which the different argument data types and return data types inherit, and use that common supertype for the arguments and return type.
 
jacob deiter
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is interesting,Any pseudo code or in dept explanation really help me lot??
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A single method can only return one type of thing. you can't define a method to return a Foo or a Bar...

but you can define a superclass or an interface, and have the Foo and Bar classes derive from the superclass or implement the interface, then define the method to return the superclass/interface type.
 
jacob deiter
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
really sorry,I am not to understand
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple example.

Suppose you have two methods, one that works on Cat objects and one that works on Dog objects:


Suppose you want to replace this with one method. The classes Cat and Dog need to have a common supertype (class or interface), and you would write the method to work on the supertype:


You can make it more type-safe with generics. In principle, the method above could return a List of other kinds of animals than the kind of animal that you pass to it. That would be strange - imagine a Dog having a list of Cats as its babies... So you could do this:


 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The return classes don't need to have a common super class or interface as long as you can cast them to or populate another object with the appropriate values. If you wanted to implement something like this you could use the following as a guide:


This approach would work well with primitive types. With more complex objects that you have control over, making them extend a common class or implement a common interface would be a better approach. Some sample code of what you're trying to do would be helpful.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a method has to be defined to return one kind of thing. you can't have a method sometimes return an int and sometimes return a String, as it would be difficult if not impossible for the calling code to know what to do. How would it know what to use to store the return value?


You can't do that. especially since the method may not know what to return until after it's been entered

but you can do this:


Now it doesn't matter. No matter what you return, it's a ParentClass object. You could do something similar by defining an interface and having your ChildA and ChildB implement the interface.
 
reply
    Bookmark Topic Watch Topic
  • New Topic