250 likes | 353 Views
Advance OO In Java. Assumption. You all have enough understanding of: Classes, and Objects some of the key concepts/features in the Object Oriented paradigm benefits of Object Oriented Design paradigm. Object Oriented Paradigm: Features. Encapsulation. Data Abstraction.
E N D
Assumption • You all have enough understanding of: • Classes,and • Objects • some of the key concepts/features in the Object Oriented paradigm • benefits of Object Oriented Design paradigm
Object Oriented Paradigm: Features Encapsulation Data Abstraction Single Inheritance OOP Paradigm Polymorphism Persistence Delegation Multiple Inheritance
Java’s OO Features Encapsulation Data Abstraction Single Inheritance Java OOP Paradigm Polymorphism Persistence Delegation Multiple Inheritance
Inheritance, Interfaces and Association • Three ways to get functionality in an object: • Implement it directly • By association • By Inheritance
A Problem Account Bond Trust Saving Account Cheque Account Govt Bond Bank Bond Insti tution Trust Private Trust Taxable
Solution: Implement Direct • Implement a method deductTax in each class publicvoid deductTax (TaxTotaller t, float taxRate) { float tax = 0.0f; tax = getBalance() * taxRate); setBalance(getBalance() * (1 - taxRate)); t.addTax(tax); } • Problems?
Solution: Association public class TaxHandler { public void deductTax(TaxTotaller tt, float taxRate, Account a) { float tax = 0.0f; tax = (a.getBalance() * taxRate); a.setBalance(a.getBalance() * (1 - taxRate)); tt.addTax(tax); }
public void deductTax(TaxTotaller tt, float taxRate, Trust t) { float tax = 0.0f; tax = (t.getBalance() * taxRate); t.setBalance(t.getBalance() * (1 - taxRate)); tt.addTax(tax); } public void deductTax (TaxTotaller tt, float taxRate, Bond b){ float tax = 0.0f; tax = (i.getBalance() * taxRate); b.setBalance(b.getBalance() * (1 - taxRate)); tt.addTax(tax); } }// end of TaxHandler
Solution: Association class ChequeAccount extends Account { privatefloat overdraftLimit = 0.0F; public ChequeAccount(float initialBalance, float initialLimit){ ... } publicboolean withdraw(float amount) { ... } // This method forwards the message to deduct tax // to a TaxHandler object: publicvoid deductTax(TaxTotaller t, float taxRate) { TaxHandler th = new TaxHandler(); th.deductTax(t, taxRate, this); } }
Problem? Stored in vector as object type acct 1 trust1 acct2 Object n Vector Cheque Account Institution Trust Cheque Account Bank Trust The real types of objects
One Solution: RTTI Object o = null; Enumeration e = v.elements(); while (e.hasMoreElements()) { o = e.nextElement(); if (ChequeAccount.class.isInstance(o)) { ((ChequeAccount)o).deductTax(t, 0.5); }elseif (BankBond.class.isInstance(o)) { ((BankBond)o).deductTax (t, 0.5); }else { ((InstitutionalTrust)o).deductTax(t, 0.5); } } • Problem?
Another Solution • Create a new inheritance hierarchy from which all taxable classes inherit and cast them to this • Use an interface interface Taxable { void deductTax(TaxTotaller t, float taxRate); } • Each object can now be cast to the type Taxable without the nested if statements.
Another Solution Contd. class ChequeAccount extends Account implements Taxable { . . . public void deductTax(TaxTotaller t, float taxRate){ ... } . . . } class BankBond extends Account implements Taxable { . . . public void deductTax(TaxTotaller t, float taxRate){ ... } . . . }
Remaining Problem • To remove the overloaded methods in TaxHandler we need to extend the interface: interface Taxable { float getBalance(); boolean setBalance(float balance); void deductTax(TaxTotaller t, float taxRate); }
Remaining Problem: Contd. class TaxHandler { publicvoid deductTax(TaxTotaller tt, float taxRate, Taxable t) { float tax = 0.0f; tax = (t.getBalance() * taxRate); t.setBalance(t.getBalance() * (1 - taxRate)); tt.addTax(tax); } }
Interface Summary • An interface defines a protocol of communication between two objects. • An interface definition is comprised of a declaration and a body. • There exist in Java, a component called an interface which only consists of method declarations i.e contains no attributes and method implementations. • This is distinct from abstract classes which can have both attributes, method implementations and constructors. • Classes can implement any number of interfaces. • Instances of classes implementing an interface can be treated as though they were instances of the interface type.
Examples: Enumeration • Enumeration interface just used in Vectors and Hashtables. This has declaration as follows: publicinterface Enumeration { boolean hasMoreElements(); Object nextElement(); } • Objects which implements them will belong to some other class
Examples: Iterators • Iterator takes the place of Enumeration in the Java collections framework • Differ from enumerations in two ways: • Allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics • Method names have been improved package java.util; publicinterface Iterator { boolean hasNext(); Object next(); void remove(); }
A nested class is a class that is a member of another class or method An inner class has full access to the implementation of the object that created it an implicit association (thisreference) to the object that created it Inner classes can be hidden from other classes in the same package (avoiding name conflicts) Nested/Inner Classes
Nested/Inner Classes public class Foo extends Bar { private int x; private int y; private A a = new A(); } class A extends B { publicvoid m() { uses x, y; } }
Example publicclass OuterClass { private String id = "OuterClass"; private String name = “OuterClass.name”; public OuterClass( ) { Innerclass IC = new InnerClass( ); } private class InnerClass { String name = "InnerClass"; publicvoid demo( ){ // access OuterClass instance variable System.out.println (id); // access InnerClass instance variable System.out.println(name); System.out.println(OuterClass.this.name); } } // InnerClass } // OuterClass
Anonymous Class • Is not given a name (which is why it is anonymous) publicclass Stack { private Vector items; ... //code for Stack's methods and constructors not shown... public Enumeration enumerator() { return new Enumeration() { int currentItem = items.size() - 1; publicboolean hasMoreElements() { return (currentItem >= 0); } public Object nextElement() { if (!hasMoreElements()) thrownew NoSuchElementException(); else return items.elementAt(currentItem--); } } } }
Anonymous Class Contd. • Class can only be instantiated once • Defined within a method of the enclosing class • May access • Class variables and methods from the enclosing class • finaldata and parameters of the enclosing method • Anonymous classes can make code difficult to read
Questions? Thank You