0% found this document useful (0 votes)
8 views12 pages

Java Object-Oriented Programming Guide

The document discusses object-oriented programming concepts in Java including method overriding, dynamic method dispatch, applying method overriding, using abstract classes, and using the final keyword with inheritance. Specifically: - Method overriding forms the basis for dynamic method dispatch where a call to an overridden method is resolved at runtime based on the object's actual type. - Abstract classes allow defining a generalized form shared by subclasses while leaving implementation of some methods to subclasses. - The final keyword can be used to prevent method overriding by making a method final, or to prevent inheritance by making a class final.

Uploaded by

Junaid khan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Java Object-Oriented Programming Guide

The document discusses object-oriented programming concepts in Java including method overriding, dynamic method dispatch, applying method overriding, using abstract classes, and using the final keyword with inheritance. Specifically: - Method overriding forms the basis for dynamic method dispatch where a call to an overridden method is resolved at runtime based on the object's actual type. - Abstract classes allow defining a generalized form shared by subclasses while leaving implementation of some methods to subclasses. - The final keyword can be used to prevent method overriding by making a method final, or to prevent inheritance by making a class final.

Uploaded by

Junaid khan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Object

Oriented
Programming
Using

JAVA
By:
Junaid Ali Siddiqui
junaid_upesh@[Link]
Method Overriding ( [Link] )
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() {
[Link]("i and j: " + i + " " + j); void show() {
} [Link]("k: " + k);
} }
}

class Override {
public static void main(String args[ ]) {
B subOb = new B(1, 2, 3);

[Link]();
}
}
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 ( [Link] )
class A { class Dispatch {
void callme() {
public static void main( String args[]) {
[Link]("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;
[Link]("Inside B's Callme [Link]();
Method");
}
r = b;
}
[Link]();
class C extends A {
void callme() { r = c;
[Link]("Inside C's Callme [Link]();
Method"); }
} }
}
Applying Method Orridding ( [Link] )
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; [Link]("Inside Area for Triangle.");
} return dim1 * dim2 / 2;
double area() { }
[Link]("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() {
[Link]("Area is " + [Link]());
[Link]("Inside Area for Rectangle.");
return dim1 * dim2; figref = t;
} [Link]("Area is " + [Link]());
}
figref = f;
[Link]("Area is " + [Link]());
}
}
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 ( [Link] )
abstract class A {
abstract void callme();
void callmetoo() {
[Link] ("This is a Concrete method.");
}
}
class B extends A {
void callme() {
[Link]("Inside B's Callme Method");
}
}
class C extends A {
void callme() {
[Link]("Inside C's Callme Method");
}
}
class AbstractDemo {
public static void main( String args[]) {
B b = new B();
[Link]();
[Link]();
}
}
Using Abstract Classes ( [Link] )
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; [Link]("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);
[Link]("Inside Area for Figure figref;
Rectangle."); figref = r;
return dim1 * dim2; [Link]("Area is " + [Link]());
} figref = t;
} [Link]("Area is " + [Link]());
}
}
Using final with Inheritance
 Using final to prevent Overriding.
 Using final to prevent Inheritance.
Using final to prevent Overriding.
class A {
final void meth() {
[Link]("This ia a final method.");
}
}

class B extends A {
void meth() { /// ERROR Can't Override
[Link]("Illegal! ");
}
}
Using final to prevent Inheritance
final class A {
// ...
}

class B extends A { // ERROR! can't extends


// ...
}
Good Luck !!!

You might also like