Sruti

Sruti's Page
Polymorphism Notes
 * The ability of a reference variable to change behavior according to what object instance it is holding.
 * The mechanism of the program deciding which method to be executed.
 * There has to be an overridden method.


 * Payment p1 ** = new ** Payment ** (50000);


 * ** Payment ** is the object reference variable
 * ** p1 ** is an object of ** Payment ** type
 * ** Payment ** is the actual object

CashPayment cp1 = newCashPayment(50000); type: CashPayment object:CashPayment variable: cp1

The type does not have to be the same as the object.

__Payment__ cp1 = new __CashPayment__ (50000);

This is possible only if CashPayment __is__ a Payment. This is possible for subclass to take from super class. -- can we say "This is possible only when the object is a sub class object with a super class type reference variable"?

CashPayment cp1 = new Payment (50000); not possible

Payment p1 = new Payment (50000); p1. paymentDetails(“Maintenance”);

The above prints “Payment is for Maintenance.”

Payment cp1 = new CashPayment (50000); p1. paymentDetails(“I am from CashPayment sub class Maintenance”);

The above prints “I am from CashPayment subclass Maintenance.”

Though the type of object variable is superclass, the subclass overridden method will be executed.

When declared as superclass type, only the superclass methods and overridden (polymorphic) methods are available.

To use creditPayment method, the object must be cast to CreditPayment.

(CreditPayment(cp2)).getCardNo; Good Notes!

Abstract Class


 * Used when you don’t have a default implementation in your super class
 * If you use an abstract method in an abstract class, it much be implemented in every class.
 * Cannot make objects with abstract class.
 * Abstract methods help programmers force thorough methods.

__Abstract class vs. Interface__


 * Abstract class has its own variable, its own implemented method, has to be a superclass for something.
 * Interface is a collection of abstract methods.

good. However, can you do this at home? If you do homework at home, you can think through all that went on in the class and you can retain most of it. It will save you a lot of revision time before the exams.

Recursion

A sophisticated looping mechanism

Must have:


 * base case – tells the recursion at what point to stop
 * logic that leads to base case


 * The strategy**


 * break problems into smaller pieces
 * some of the pieces are dealt immediately (base cases)
 * others need to be further broken up

public static int triangle (int n)

{

if (n==1)

return 1;

else

return n + triangle (n-1);

}

Examples:

n + [n -1] Triangle (4)

4 + 3 + (3-1) 4 + Triangle (3)

4 + 3 + 2 + (2-1) 4 + 3 + Triangle (2)

4 + 3 +2 + Triangle (1)

n + (Triangle (n – 1))

base case

n == 1 good notes!.. all problem which can be solved through recursion can be solved through itermation..