2

The Base class will receive an unknown data structure via Data type. The Derived class needs to override the printObj method in order to print the content of the Data type object. For this example, the Data type object contains one field which is a string. How to override the printObj() to print the content of String message variable?

// generic class
public abstract class Base <T>{
    T obj;
    public Base(T t){
        obj = t;
    }
    public T getObject(){
        return obj;
    }
    public void setObject(T t){
        obj = t;
    }
    public abstract void printObj();
}
// specific class
public class Derived<T> extends Base<T>{
    public Derived(T t) {
        super(t);
    }
    @Override
    public void printObj() {
        //?????
    }
}
// data can be changed
public class Data {
    String message;
    public Data(String msg){
    message = msg;
    }
}
// application class
public class App {
    public static void main(String[] args) {
        Data d = new Data("Hello");
        Derived<Data> dClass = new Derived<Data>(d);
        dClass.printObj();
    }
} 
2
  • 1
    What have you tried? This is a basic concept of generics that has literally thousands of explanations with a simple google search. Commented Jun 4, 2014 at 21:44
  • What do you want to happen if you do Derived<int[]> dClass2 = new Derived<int[]>(new int[] {1, 2, 3, 4, 5}); dClass2.printObj();? Commented Jun 5, 2014 at 6:55

2 Answers 2

3

The T in Derived<T> could be anything, so the compiler doesn't know that it has a message field. Maybe you wanted

public class Derived<T extends Data> extends Base<T>

then you could have

@Override
public void printObj() {
    whatever(obj.message);
}
Sign up to request clarification or add additional context in comments.

4 Comments

It can't be extended in future if needed. Not a good idea. You have bind it to Data whereas only one functionality is needed to separate out.
what can't be extended?
Can I pass any object that is not of type Data?
we came into this question with very different ideas of what the asker was trying to build.
1

Strategy design pattern

In computer programming, the strategy pattern (also known as the policy pattern) is a software design pattern that enables an algorithm's behavior to be selected at runtime.

The strategy pattern

  • defines a family of algorithms,
  • encapsulates each algorithm, and
  • makes the algorithms interchangeable within that family.

Why Use Patterns?

  • They have been proven. Patterns reflect the experience, knowledge and insights of developers who have successfully used these patterns in their own work.
  • They are reusable. Patterns provide a ready-made solution that can be adapted to different problems as necessary.
  • They are expressive. Patterns provide a common vocabulary of solutions that can express large solutions succinctly.

You can solve this problem using above mentioned design pattern easily by using a simple interface that separate out the printable functionality/algorithm. Any object of this type will be called the printable object.

interface Printable{public void print();}

Now it should be like this T extends Printable that means any object of type Printable

abstract class Base<T extends Printable> implements Printable{...}

Now T extends Printable so you can downcast it to Printable and can call print() method on it that will call the method of the passed printable object.

class Derived<T extends Printable> extends Base<T> {
    ...
    @Override
    public void print() {
        ((Printable)getObject()).print();
    }
}

Here Data is a printable object by simply implementing Printable interface.

class Data implements Printable {
    String message;
    ...
    @Override
    public void print() {
        System.out.println(message);
    }
}

Lets create one more class to understand the benefit of using this design pattern that's not possible by using T extends Data.

class NonData implements Printable {
    String message;
    ...
    @Override
    public void print() {
        System.out.println(message);
    }
}

Main class:

Data d = new Data("Hello");
Derived<Data> dClass = new Derived<Data>(d);
dClass.printObj(); // output Hello


NonData d1 = new NonData("Bye");
Derived<NonData> dClass1 = new Derived<NonData>(d1);
dClass1.printObj(); // output Bye

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.