3

I have a scenario that i want to create one support class called D which contains a generic method. I have set the upper bound for type variable.

class A{
    void show(){
        System.out.println("Hello A");
    }
}

class B extends A{
    void msg(){
        System.out.println("Hello B");
    }
}

class C extends A{
    void msg(){
        System.out.println("Hello C");
    }
}

class D{
    <T extends A> void display(T ob){
        ob.msg(); //here i want to do some tricks
    }
}

First i want to share my objective. Here msg() function of B and C class has different implementations. I want to create one support class called D that has one display method, using display method i want to call either msg() function of B or C class dependent on instantiation. Can you tell me how can i achieve it?

8
  • 1
    Just FYI, these are methods, not functions. Commented Jun 30, 2017 at 6:54
  • Can;t you check to see if ob instanceof B || ob instanceof C? Or maybe thats not what you are asking?? Commented Jun 30, 2017 at 6:55
  • 1
    class A don't have a msg method, it is only declare in the subclass. So if A can be abstract, add an abstract method, if not, add an empty method (not good in the first place...). Or rename show in A to match the methods name in B and C (if they should be the same) Commented Jun 30, 2017 at 6:55
  • One of options: introduce an interface M{void msg();} which will be implemeted by B and C, so no generics required. Commented Jun 30, 2017 at 6:56
  • @kan but using T extends A, T will only "see" the methods of A, if the interface is only implemented in B and C, this won't work. It need to be implemented in A. Commented Jun 30, 2017 at 6:57

3 Answers 3

2

You need to have the method msg() in class A, otherwise the display() method in class D does not know if this method exist or not in the object that you're passing to it. (What if someone makes a class E that extends A but does not have a msg() method, and you pass an E to D.display()?).

If you don't want to implement the msg() method in class A, then you can make it abstract (and you'll also have to make the class abstract).

abstract class A {
    public abstract void msg();

    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

more like an architecture style, I would use an interface for that, so your generic method constrains to <T extends If> void display(T ob) where If is the interface with the abstract method msg

interface If {
    void msg();
}

class A {
    void show() {
        System.out.println("Hello A");
    }
}

class B extends A implements If {
    @Override
    public void msg() {
        System.out.println("Hello B");
    }
}

class C extends A implements If {
    @Override
    public void msg() {
        System.out.println("Hello C");
    }
}

class D {
    <T extends If> void display(T ob) {
        ob.msg(); // here i want to do some tricks
    }
}

3 Comments

Isn't it changing the method definition ? You can't pass an A instance anymore. But indeed, this is a clean solution
OP doesnt need an A object, OP needs something that can implement the msg method
What I mean is you can't use it like A a = new B(); new D().display(a);.What the original definition was accepting. And of course, you lose the possibilities to use A.show(). Not a problem here, but this need to be point out ;)
0

You don't need generics for this, there is basic concept called dynamic binding in Java.

abstract class A{
    void show(){
        System.out.println("Hello A");
    }       
     abstract void msg();    
}

class B extends A{
    @Override
    void msg(){
        System.out.println("Hello B");
    }
}

class C extends A{

       @Override
       void msg(){
             System.out.println("Hello C");
       }
}

class D{
    void display(A ob){
        ob.msg(); 
    }
}

Here an appropriate instance provided to method will determine which class method should in called at runtime.

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.