Java Class, Objects, Methods, Constructor example – part 3


11. Example 11:

class AB{
int a;//instance field
static int b;//class field
}

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

AB ab1=new AB();
AB ab2=new AB();
AB ab3=new AB();

ab1.a=10;
ab2.a=20;
ab3.a=30;
ab2.b=40;
ab3.b=AB.b+ab1.b;

System.out.println(ab1.a+ab2.b);

}
}

Note* : Save, Compile and Execute
Output:
90

12. Example 12:
class B{
B(int n){
System.out.println(n);
}
void display(){
System.out.println(“In display of b”);
}
}

class C extends B{
C(int b){
super(b);
System.out.println(b+10);
}

void display(){
System.out.println(“In display of c”);
super.display();
}
}

class A{
public static void main(String[] args){
C obj=new C(10);
obj.display();
}
}

 

Note* : Save, Compile and Execute
Output:
10

20
In display of c
In display of b