aspose file tools*
The moose likes Beginning Java and the fly likes Design single method with different input type and different data type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Design single method with different input type and different data type" Watch "Design single method with different input type and different data type" New topic
Author

Design single method with different input type and different data type

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
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?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Overloading . and if you give different number of parameter then it make sense. but use overloading judiciously because it bit confuse
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
it is interesting,Any pseudo code or in dept explanation really help me lot??
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
really sorry,I am not to understand
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

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:


Jarred Olson
Ranch Hand

Joined: Jul 31, 2009
Posts: 37
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
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Design single method with different input type and different data type