0

So lets say i have 2 types of objects, and I won't know ahead of time which object will be used. I want too apply whichever object is used to do the same things in a specific method. How can i do this without duplicating code?

ex:

if (input=1){
    version1 version = new version1()
}
else{
    version2 version = new version2()
}

**do something with version**

obviously this way won't compile, but what would i need to do to accomplish what i am trying to achieve?

Maybe I have to do a try catch? sorry I am still new to java.

TheJavaBeast
  • 163
  • 3
  • 16
  • 2
    What's the relation of `version1` and `version2`? Do they implement the same interface? To they have a common ancestor class? What code duplication are you trying to avoid? – Eran Aug 08 '14 at 17:36
  • 1
    Check out [Abstract Classes](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html) and [Interfaces](http://docs.oracle.com/javase/tutorial/java/concepts/interface.html) – Dan Aug 08 '14 at 17:39
  • You probably also want `if (input == 1) {` instead of what you currently have: `if (input = 1) {` – Zach Aug 08 '14 at 17:39
  • in my real life problem the difference is they are different DOA java classes/ objects that are derived from the same table and can be considered identical for our purpose. I am trying to avoid having two separate methods to call that do the same thing – TheJavaBeast Aug 08 '14 at 17:40

5 Answers5

13

You can have a superclass called Version.
Version1 and Version2 extend this superclass.

Then you can do as follows:

Version version = null; // superclass reference
if (input == 1){
    version = new Version1(); // instance of subclass Version1
}
else{
    version = new Version2(); // instance of subclass Version2
}
// Now you can use the object version.

This is called Runtime Polymorphism in Java - using superclass reference to point to the sub classes. (Because of this, you don't need to know ahead of time which object will be used, as you mentioned in your question :)).

Alternatively, You can also use an Interface called Version instead of the superclass Version. In this case Version1 and Version2 implement Version instead of extend it.

Note:
I suggest you read about Inheritance and Polymorphism in Java.
Also try to clearly understand what are Interfaces, Superclasses, etc.
Here is a SO link which answers the question 'when to use a superclass vs Interface'.

Community
  • 1
  • 1
rgamber
  • 5,749
  • 10
  • 55
  • 99
  • 1
    both of my DOA version classes already extend the same class. So regardless of what that class does i can use it in your example to connect the two? – TheJavaBeast Aug 08 '14 at 17:50
  • That's right. If they already extend the same Class, then use that calss to instantiate the objects of subclasses. Google for 'Runtime Polymorphism' in Java. That will explain you how it works! – rgamber Aug 08 '14 at 17:52
  • 1
    When i try this the code looks for the methods called(that reside in the version class) in their super class... they don't exist there – TheJavaBeast Aug 08 '14 at 18:00
  • I am not sure I understand what you mean by 'methods called in version classes in their superclass'? – rgamber Aug 08 '14 at 18:02
  • sorry. Both of the versions have methods defined in their class. When i try to access these methiods the code now looks to the class they are extending or wants me to cast the call to a specific version class – TheJavaBeast Aug 08 '14 at 18:05
  • Spend some time to understand how Runtime Polymorphism works in Java. That is the answer to your question. Also it will be beneficial to you in the long run instead of taking the shortcut. Otherwise you will keep running into questions, and this chat will be endless :) – rgamber Aug 08 '14 at 18:10
  • To quickly answer your question: If Version has a method called test(), and Version1 and Version2 both have thier own implementation of test() as well, then `Version version = new Version1()` will call Version1's test(). (Note: class variables behave differently). – rgamber Aug 08 '14 at 18:18
4

Is this what you are looking for?

public class Version1 implements Version{

//Version 1 stuff
}

public class Version2 implements Version{

//Version 2 stuff
}

then your code:

Version version =null
if (input==1){
version = new Version1()
}
else{
version = new Version2()
}

//Do something with version
Droidekas
  • 3,464
  • 2
  • 26
  • 40
0

How familiar are you with the concept of Interfaces? You can have 2 classes Version1, Version2 implement the Version interface and do something like this:

Version version;

version = (input == 1)? new Version1() : new Version2();

You can read about Interfaces here : Link

gkrls
  • 2,618
  • 2
  • 15
  • 29
0

You can create an interface, and then implement the interface in two different versions.

So you would have:

public interface InterfaceName {

    public int method1();
}

public class version1 implements InterfaceName {

    public int method1() {
        //code for method1
    }
}

public class version2 implements InterfaceName {

    public int method1() {
        //code to implement in a different way if needed
    } 
}
haley
  • 1,165
  • 7
  • 13
0

You can make the classes implement a particular interface and put your specific version code in the interface method implementor

class Version1 implements VersionInterface{

     public void versionCode(){
/// your Version1 class specific code
  }
}

class Version2 implements VersionInterface{

     public void versionCode(){
/// your Version1 class specific code
  }
}

VersionInterface version =null;
if (input=1){
    version = new Version1()
}
else{
    version = new version2()
}

version.versionCode(); // Runtime polymorphism 
Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35