简体   繁体   中英

Java: use getters and setters with polymorphism

I have for example these classes:

public class A {
}

public class B {
}

public class Parent {
    private A a;
    public A getA() {
        return a;
    }
    public void setA(A a) {
        this.a = a;
    }
}

public class Child extends Parent {
    private B b;
    public B getB() {
        return b;
    }
    public void setB(B b) {
        this.b = b;
    }
}

And in my Main class I would like to use polymorphism: Parent p = new Child(); . I need to use setA and setB functions both on this p element. What is the best way to do this? I see right now just the setA function because of the polymorphism - so do I have to override the setters and getters in the child class? Is it recommended?

Java uses dynamic binding, in-order to decide which methods be accessible, so when you want to access methods of another class you have to cast it to that class. In your case when you want to access methods of class Child, you can explicitly cast your obj to Child and then call your methods:

     Parent p = new Child();
    ((Child) p).getB();
import java.util.Scanner;

public class Calculator {
public static void main(String []args){

for(int count=1;count<=200;count++){
    Scanner x=new Scanner(System.in);

    System.out.println("1 for sum\n2 for sub\n3 for div\n4 for multi");
    int a=x.nextInt();

    double fnum;
    double snum;
    switch(a){

    case 1:
            System.out.println("enter first number");
            fnum=x.nextDouble();
            System.out.println("enter second number");
            snum=x.nextDouble();
            Sum s=new Sum(fnum,snum);
            System.out.println("your Sum is "+s.add(fnum, snum));
            break;

    case 2:
            System.out.println("enter first number");
            fnum=x.nextDouble();
            System.out.println("enter second number");
            snum=x.nextDouble();
            Sub d=new Sub(fnum,snum);
            System.out.println("the difference is "+d.min(fnum, snum));
            break;

    case 3:
            System.out.println("enter first number");
            fnum=x.nextDouble();
            System.out.println("enter second number");
            snum=x.nextDouble();
            Div c=new Div(fnum,snum);
            System.out.println("the qoutient is "+c.div(fnum, snum));
            break;  
    case 4:
            System.out.println("enter first number");
            fnum=x.nextDouble();
            System.out.println("enter second number");
            snum=x.nextDouble();
            Mult m=new Mult(fnum,snum);
            System.out.println("the product is "+m.tim(fnum, snum));
            break;
    default:
            System.out.println("invalid input");
            x.next();
            break;


            }
        }
    }
}

Polymorphism means one method name but different uses to achieve polymorphism in this 2 classes you need to override your Parent class setA() method to do this you must call the super keyword inside your Child class like this

public void setA(A a)
{
  super.setA(A a);

}

note that super is only available in non static method of Child class

if you want to set your b in your setA() method you can just create an overloaded method meaning same name but different parameter list, this is also polymorphic in nature

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM