0

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?

3
  • 2
    I suggest you research inheritance on the internet. Loads of examples like this to be found. Perhaps this explains your question a bit? stackoverflow.com/questions/6182141/java-inheritance-example Commented Jul 2, 2020 at 12:17
  • You need to make use of constructors to achieve what you are expecting in a more cleaner way Commented Jul 2, 2020 at 12:18
  • Your question is rather about the Inheritance rather than Generics of Java. Commonly speaking, any sub-type instance can be assigned to their parent type variable. Commented Jul 2, 2020 at 12:19

2 Answers 2

2

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.

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

Comments

1

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.

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.