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.

4
  • 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 8, 2014 at 17:36
  • 1
    Check out Abstract Classes and Interfaces
    – Dan
    Aug 8, 2014 at 17:39
  • You probably also want if (input == 1) { instead of what you currently have: if (input = 1) {
    – Zach
    Aug 8, 2014 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 Aug 8, 2014 at 17:40

5 Answers 5

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'.

7
  • 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? Aug 8, 2014 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 8, 2014 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 Aug 8, 2014 at 18:00
  • I am not sure I understand what you mean by 'methods called in version classes in their superclass'?
    – rgamber
    Aug 8, 2014 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 Aug 8, 2014 at 18:05
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
2
  • Version version should be declared outside and before the if-else. Check @rgamber post. Aug 8, 2014 at 17:43
  • I wanted to write the same code..but u wrote it quicker.:) Aug 8, 2014 at 17:45
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

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
    } 
}
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 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.