2016-10-13

posted Oct 13, 2016, 6:55 AM by Samuel Konstantinovich   [ updated Oct 13, 2016, 10:29 AM ]
Goal: I want my inheritance!!!!

Think of how the classes A and B interact:

public class A{
    private int x;
    public A(){
System.out.println("A constructor!()");
    }    
    public A(int x){
System.out.println("A (n)other constructor!(int)");
this.x = x;
    }
    public int getX(){
return x;
    }
    public void yell(){
System.out.println("Hey yous guys "+getX());
    }
}





public class B extends A{
    public B(){
super(1);
System.out.println("B constructor");
    }
    public int getX(){
return super.getX()*-1;
    }
}



public class Driver{
    
    public static void main(String[]args){
System.out.println("ObA:");
A obA = new A(2);
System.out.println("ObB:");
B obB = new B();
System.out.println(obA.getX());
System.out.println(obB.getX());
obA.yell();
obB.yell();
    }
}



Comments