Java 2

  • Uploaded by: Junaid khan
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Java 2 as PDF for free.

More details

  • Words: 767
  • Pages: 12
Object Oriented Programming Using

JAVA By:

Junaid Ali Siddiqui [email protected]

Method Overriding ( Override.java ) class A { int i,j; A(int a, int b) { i = a; j = b; } void show() { System.out.println("i and j: " + i + " " + j); } }

class B extends A { int k; B(int a, int b, int c) { super(a, b); k = c; } 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 { void callme() { System.out.println("Inside A's Callme Method"); } }

class Dispatch { public static void main( String args[]) { A a = new A(); B b = new B(); C c = new C(); A r;

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"); } }

r = a; r.callme(); r = b; r.callme(); r = c; r.callme(); } }

Applying Method Orridding ( FindAreas.java ) class Figure { double dim1; double dim2; Figure(double a, double b) { dim1 = a; dim2 = b; } double area() { System.out.println("Area for Figure is undefined."); return 0; } } class Rectangle extends Figure { Rectangle(double a, double b) { super(a, b); } double area() { System.out.println("Inside Area for Rectangle."); return dim1 * dim2; } }

class Triangle extends Figure { Triangle (double a, double b) { super(a, b); } double area() { System.out.println("Inside Area for Triangle."); return dim1 * dim2 / 2; } } class FindAreas { public static void main(String args[]) { Figure f = new Figure(10, 10); Rectangle r = new Rectangle(9,5); Triangle t = new Triangle(10, 8); Figure figref; figref = r; System.out.println("Area is " + figref.area()); 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 { double dim1; double dim2; Figure(double a, double b) { dim1 = a; dim2 = b; } abstract double area(); } class Rectangle extends Figure { Rectangle(double a, double b) { super(a, b); } double area() { System.out.println("Inside Area for Rectangle."); return dim1 * dim2; } }

class Triangle extends Figure { Triangle (double a, double b) { super(a, b); } double area() { System.out.println("Inside Area for Triangle."); return dim1 * dim2 / 2; } } class AbstractAreas { public static void main(String args[]) { //Figure f = new Figure (10, 10); // illegal now Rectangle r = new Rectangle(9,5); Triangle t = new Triangle (10, 8); Figure figref; figref = r; 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 !!!

Related Documents

2 Java
November 2019 10
Java 2
April 2020 12
Java 2
April 2020 12
Java 2
June 2020 12
Java 2
May 2020 14

More Documents from ""

Bubble Sort
May 2020 22
Java 2
May 2020 14
Junaid (quick Sort)
April 2020 9
Binary Search
May 2020 11