Gurunand A
Gurunand A

Reputation: 11

How do we pass objects of generic class to a method in Java

I have two class as follows-

public class Test1 {
  private int a;
  private int b;
  public setA(int a) {this.a=a;}
  public setB(int b) {this.b=b;}
}

public class Test2 extends Test1 {
  private int c;
  public setC(int c) {this.c=c;}
}

I am looking for a method to call which will set the values of a and b, if i pass an object of either Test1 or Test2

public class Test3 {
  Test1 obj1 = new Test1();
  Test2 obj2 = new Test2();

  public static void main() {
    populateAB(obj1);
    populateAB(obj2);
  }
  
  public void populateAB(???something obj) {
    obj.setA(5);
    obj.setB(10);
  }
}

What should be the prototype of populateAB() to accept objects from both Test1 and Test2 and call the setter functions?

Upvotes: 0

Views: 67

Answers (2)

René
René

Reputation: 398

Since Test2 inherits from Test1 you can simply use Test1 as the datatype.

public class Main {

    public static void main(String[] args) {
        Test1 obj1 = new Test1();
        Test2 obj2 = new Test2();

        populateAB(obj1);
        populateAB(obj2);

        System.out.println(obj1);
        System.out.println(obj2);
    }

    public static void populateAB(Test1 obj) {
        obj.setA(5);
        obj.setB(10);
    }
}

class Test1 {
    private int a;
    private int b;
    public void setA(int a) {this.a=a;}
    public void setB(int b) {this.b=b;}

    @Override
    public String toString() {
        return String.format( "[a = %d; b = %d ]", a, b );
    }
}

class Test2 extends Test1 {
    private int c;
    public void setC(int c) {this.c=c;}
}

Output:

[a = 5; b = 10 ]
[a = 5; b = 10 ]

Its the same with the toString() Method which is part of the Object class but every Object inherits from the Object class so they have access to those methods and also can be stored all together in an Object[] or something

Another example:

public class Main {

    public static void main( String[] args ) {
        Dog dog = new Dog( "Bello" );
        Cat cat = new Cat( "Garfield" );

        Animal[] animals = new Animal[] { dog, cat };

        for ( Animal animal : animals ) {
            System.out.println( animal.name );
        }
    }
}

class Animal {
    public String name;

    Animal( String name ) {
        this.name = name;
    }
}

class Cat extends Animal {

    Cat( String name ) {
        super( name );
    }
}

class Dog extends Animal {

    Dog( String name ) {
        super( name );
    }
}

Output:

Bello
Garfield

As you can see Im storing two different objects together as Animals since they both inherit from the Animal class.

Upvotes: 1

Marcus Lanvers
Marcus Lanvers

Reputation: 411

If you set ???something to Test1 you will be able to call obj.setA() and obj.setB(). As you are using inheritance you will be able to call populateAB with and object of class Test1 and with an object of class Test2. But you won't be able to use the setter or getter of the property c of class Test2.

Upvotes: 2

Related Questions