Jumat, 10 Agustus 2012

Penggunaan Extends

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tugaskuliah;

/**
 *
 * @author tamam
 */
class A {
protected int v=1;
protected static int x=0;
A(){
v++;
x++;
}
public void print(){
System.out.println("A.v = " + v);
}
}

class B extends A {
//private int v=0;
B(){
//super();
v= x+v+2;
}
public void print(){
super.print();
System.out.println("B.v = " + v);
}
public void printX(){
System.out.println("B.x = " + ++x);
}
}
public class Upcasting {
public static void main(String[] args) {
A a = new A();
B b = new B();
a = b;

a.print();
b.printX();
}
}

//hasilnya ...
//A.v = 6
//B.v = 6
//B.x = 3

Tidak ada komentar: