Object
Oriented
Programming
Using
JAVA
By:
Junaid Ali Siddiqui
[email protected]
Method Overriding ( Override.java )
class A { class B extends A {
int i,j; int k;
A(int a, int b) {
i = a; B(int a, int b, int c) {
j = b; super(a, b);
} k = c;
}
void show() {
System.out.println("i and j: " + i + " " + j); void show() {
} System.out.println("k: " + k);
} }
}
class Override {
public static void main(String args[ ]) {
B subOb = new B(1, 2, 3);
subOb.show();
}
}
Dynamic Method Dispatch
Method Overriding forms the basis for one of Java’s
most powerful concepts: Dynamic Method Dispatch.
Dynamic Method Dispatch is the mechanism by which
a call to an overridden method is resolved at run time.
An important principle: a superclass reference
variable can refer to a subclass object.
Java uses this fact to resolve calls to overridden
methods at run time.
Dynamic Method Dispatch ( Dispatch.java )
class A { class Dispatch {
void callme() {
public static void main( String args[]) {
System.out.println("Inside A's Callme
Method"); A a = new A();
} B b = new B();
} C c = new C();
A r;
class B extends A {
void callme() { r = a;
System.out.println("Inside B's Callme r.callme();
Method");
}
r = b;
}
r.callme();
class C extends A {
void callme() { r = c;
System.out.println("Inside C's Callme r.callme();
Method"); }
} }
}
Applying Method Orridding ( FindAreas.java )
class Figure { class Triangle extends Figure {
double dim1; Triangle (double a, double b) {
double dim2; super(a, b);
Figure(double a, double b) { }
dim1 = a; double area() {
dim2 = b; System.out.println("Inside Area for Triangle.");
} return dim1 * dim2 / 2;
double area() { }
System.out.println("Area for Figure is undefined."); }
return 0; class FindAreas {
} public static void main(String args[]) {
} Figure f = new Figure(10, 10);
class Rectangle extends Figure { Rectangle r = new Rectangle(9,5);
Rectangle(double a, double b) { Triangle t = new Triangle(10, 8);
super(a, b); Figure figref;
}
figref = r;
double area() {
System.out.println("Area is " + figref.area());
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2; figref = t;
} System.out.println("Area is " + figref.area());
}
figref = f;
System.out.println("Area is " + figref.area());
}
}
Using Abstract Classes
In situations, when we don’t need the complete
implementation of every method in superclass.
Superclass only defines a generalized form that will
be shared by all of its subclasses, leaving it to each
subclass to fill in the detail.
Abstract keyword is used for that perpose.
These methods are sometime reffered to as
subclasses responsibilty because they have to no
implementation specified in the superclass.
A class having one or more abstract methods are
called abstract classes.
Using Abstract Classes ( AbstractDemo.java )
abstract class A {
abstract void callme();
void callmetoo() {
System.out.println ("This is a Concrete method.");
}
}
class B extends A {
void callme() {
System.out.println("Inside B's Callme Method");
}
}
class C extends A {
void callme() {
System.out.println("Inside C's Callme Method");
}
}
class AbstractDemo {
public static void main( String args[]) {
B b = new B();
b.callme();
b.callmetoo();
}
}
Using Abstract Classes ( AbstractAreas.java )
abstract class Figure { class Triangle extends Figure {
double dim1; Triangle (double a, double b) {
double dim2; super(a, b);
Figure(double a, double b) { }
dim1 = a; double area() {
dim2 = b; System.out.println("Inside Area for Triangle.");
} return dim1 * dim2 / 2;
abstract double area(); }
} }
class Rectangle extends Figure { class AbstractAreas {
Rectangle(double a, double b) { public static void main(String args[]) {
super(a, b); //Figure f = new Figure (10, 10); // illegal now
} Rectangle r = new Rectangle(9,5);
double area() { Triangle t = new Triangle (10, 8);
System.out.println("Inside Area for Figure figref;
Rectangle."); figref = r;
return dim1 * dim2; System.out.println("Area is " + figref.area());
} figref = t;
} System.out.println("Area is " + figref.area());
}
}
Using final with Inheritance
Using final to prevent Overriding.
Using final to prevent Inheritance.
Using final to prevent Overriding.
class A {
final void meth() {
System.out.println("This ia a final method.");
}
}
class B extends A {
void meth() { /// ERROR Can't Override
System.out.println("Illegal! ");
}
}
Using final to prevent Inheritance
final class A {
// ...
}
class B extends A { // ERROR! can't extends
// ...
}
Good Luck !!!