Advance Computer Programming: by Hasnat Ali
Advance Computer Programming: by Hasnat Ali
By
Hasnat Ali
Inheritance, Polymorphism, and
Encapsulation
Inheritance
Inheritance in JAVA
public class A{
private int var1;
public int var2;
A(){ System.out.println(“Constructor A"); }
}
public class B extends A{
private int var3;
B(){ System.out.println(“ Constructor B"); }
}
public class C extends B{
private int var4;
C(){ System.out.println(“ Constructor C"); }
}
class A{
A(){ System.out.println("A"); }
}
class B extends A{
B(int i){
// by default, here the first statement is super();
System.out.println("B");
}
}
class Test{
public static void main(String args[]){
B b = new B(5);
}
}
About Super Class’s Constructor
class A{
A(int i){
System.out.println("A");
}
}
class B extends A{
B(int i){
// by default, here the first statement is super();
System.out.println("B");
}
}
class Test{
public static void main(String args[]){
B b = new B(5);
}
}
Output:
Error, Required int argument, Found no arguments
About Super Class’s Constructor
class A{
A(int i){
System.out.println("A");
}
}
class B extends A{
B(int i){
super(i); // explicitly pass integer argument
System.out.println("B");
}
}
class Test{
public static void main(String args[]){
B b = new B(5);
}
}
Use of super Keyword
Caution
You must use the keyword super to call the superclass
constructor. Invoking a superclass constructor’s name in a
subclass causes a syntax error.
Java requires that the statement that uses the keyword super
appear first in the constructor.
Constructor Chaining
class Animal {
public Animal(String name) {
System.out.println(“Animal Class Constructor");
}
}
Object Class in Java
equals()
wait()
finalize()
getClass()
hashCode()
notify()
notifyAll()
toString()
Polymorphism
(Dynamic Method Dispatch)
Polymorphism and Method Overriding
public class A{
void callMe() { System.out.println(“ A’s callMe() Method”); }
}
public class B extends A{
void callMe() { System.out.println(“ B’s callMe() Method"); }
}
public class C extends B{
void callMe() { System.out.println(“ C’s callMe() Method"); }
}
Cn Cn-1 ..... C2 C1
public class A{
void callMe() { System.out.println(“ A’s callMe() Method”); }
}
public class B extends A{
void callMe() { System.out.println(“ B’s callMe() Method"); }
}
public class C extends B{
// void callMe() { System.out.println(“ C’s callMe() Method"); }
}
class B { class B {
public void p(double i) { public void p(double i) {
System.out.println(i * 2); System.out.println(i * 2);
} }
} }
private
default (none)
protected
public
Visibility increases
public
protected -
default - -
private - - -
package p1; package p2;
public class C1 { public class C2 { public class C3 {
public int x; void aMethod() { void aMethod() {
int y; C1 o = new C1(); C1 o = new C1();
private int z; can access o.x; can access o.x;
can access o.y; cannot access o.y;
public void m1() { cannot access o.z; cannot access o.z;
}
void m2() { can invoke o.m1(); can invoke o.m1();
} can invoke o.m2(); cannot invoke o.m2();
private void m3() { cannot invoke o.m3(); cannot invoke o.m3();
} } }
} } }
package p1;
public class C1 { public class C2 {
public int x; C1 o = new C1();
protected int y; can access o.x;
int z; can access o.y;
private int u; can access o.z;
cannot access o.u;
protected void m() {
} can invoke o.m();
} }
package p2;
Note: The getter and setter methods are used to read and
modify private properties.
Note
An object cannot access its private members, as shown in (b). It is correct,
however, if the object is declared in its own class, as shown in (a).
Visibility increases