0% found this document useful (0 votes)
31 views

JAVA Modifier Inheritance

1. The Foo, Bar, Baz, and Mumble classes demonstrate method overriding and polymorphism. 2. When an array of Foo objects is created, calling methods on each object invokes the most specific implementation based on the actual object type. 3. The output shows the overridden method implementations are called on the subclass objects instead of the parent Foo class methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

JAVA Modifier Inheritance

1. The Foo, Bar, Baz, and Mumble classes demonstrate method overriding and polymorphism. 2. When an array of Foo objects is created, calling methods on each object invokes the most specific implementation based on the actual object type. 3. The output shows the overridden method implementations are called on the subclass objects instead of the parent Foo class methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

public class A { Result:

public int x = 1; 1
public void setX(int a){ 2
x=a;
} Public instance variable and
}
instance method can be
public class B extends A {
inherited and accessed by
public int getB(){
setX(2); subclass (without
return x;} overriding)
}
public class C {
public static void main(String [] args){
A a = new A();
B b = new B();
System.out.println(a.x);
System.out.println(b.getB());
}
}
public class A { Result:
private int x = 1; 1
protected void setX(int a){ 2
x=a;
} Private instance variable
protected int getX(){
and private instance
return x;}
} methods can be inherited
public class B extends A { but not accessible to
public int getB(){ subclass!
setX(2);
//return x; It does not work because private modifier, so Protected instance variable
return getX(); and protected instance
} methods can be inherited
} and accessible to subclass,
public class C {
public static void main(String [] args){
A a = new A();
B b = new B();
System.out.println(a.getX());//a.x is not allowed, private!
System.out.println(b.getB());
}
}
public class A { Result
protected int x = 1; 1
protected void setX(int a){x=a;} 1
protected int getX(){return x;} 2
}
public class B extends A {
*The difference of B’s x is
public int getB(){
not variable shadowing. It’s
setX(2);
return x; the expected execution of
} value resetting (setX(2)).
}
public class C {
public static void main(String [] args){
A a = new A();
B b = new B();
System.out.println(a.getX());
System.out.println(b.x); //b.x is protected, then inherited.
System.out.println(b.getB());
}
}
public class A { Resutls:
protected int x = 1; 1
protected void setX(int a){ 3
x=a; 3
} 1
protected int getX(){
3
return x;}
}
public class B extends A { Do you know which getX of
protected int x = 3; b is called, A’s or its own? If
public int getX(){ you cannot ensure your
return x; } answer right, please see the
public int getB(){ comment in the below.
return x;
}
}
public class C {
public static void main(String [] args){
A a = new A();
B b = new B();
System.out.println(a.getX());//target for comparison
System.out.println(b.getB());//subclass method access own attrib
System.out.println(b.getX());//overriding method, accessing sub
System.out.println(a.x); //protected
System.out.println(b.x); //overriding attribute!
}
}
//reusing the above class A and B Results:
public class C { 1
public static void main(String [] args){ 3
A a = new A(); 1
A b = new B(); //polymorphism, making shadowing possible! 1
System.out.println(a.getX());
System.out.println(b.getX());//override, access subclass attri.
//System.out.println(b.getB()); not able to load subclass method! Subclass variable can be
System.out.println(a.x); accessed by method, the
System.out.println(b.x); //variable shadowing! direct access (without using
} method) will reach the
} overridden value from
superclass!
// For your development:
//1) Is it good to block the use of b.getB()? b.getB is not permitted
// ANS>: Good, because methods can be in template. In the security control, no leakage! because it is out A’s
//2) Is it good to have the direct access of attribute such as b.x? signature. b.getX is allowed
// ANS>: Better not, if it is not in your control. See how complicate it is in this program. because it is overridden!
// reusing the above class A and C Results:
public class B extends A { 1
protected int x = 3; 3
public int getX(){ 1
setX(2); // call superclass method to set 2 in superclass attrib 2
return x; } //but return attrib of subclass
public int getB(){
return x; b.x is set to 2 because a
} superclass method is called
} to change the value of
shadowed value.
public class Foo {
public void method1() {
System.out.println("foo 1");
}
public void method2() {
System.out.println("foo 2");
}
public String toString() {
return "foo";
}
}
public class Bar extends Foo {
public void method2() {
System.out.println("bar 2");
}
}

public class Baz extends Foo {


public void method1() {
System.out.println("baz 1");
}
public String toString() {
return "baz";
}
}
public class Mumble extends Baz { baz
public void method2() { baz 1
System.out.println("mumble 2"); foo 2
}
} foo
foo 1
public class Polymorphism{ bar 2
public static void main(String [] args){
Foo[] pity = { new Baz(), new Bar(), baz
new Mumble(), new Foo() }; baz 1
for (int i = 0; i < pity.length; i++) { mumble 2
System.out.println(pity[i]);
pity[i].method1(); foo
pity[i].method2(); foo 1
System.out.println(); foo 2
}
} *( ) method is inherited. Otherwise, method is overridden.

You might also like