1 / 25

Advance OO In Java

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.

Download Presentation

Advance OO In Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advance OO In Java

  2. 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

  3. Object Oriented Paradigm: Features Encapsulation Data Abstraction Single Inheritance OOP Paradigm Polymorphism Persistence Delegation Multiple Inheritance

  4. Java’s OO Features Encapsulation Data Abstraction Single Inheritance Java OOP Paradigm Polymorphism Persistence Delegation Multiple Inheritance

  5. Inheritance, Interfaces and Association • Three ways to get functionality in an object: • Implement it directly • By association • By Inheritance

  6. A Problem Account Bond Trust Saving Account Cheque Account Govt Bond Bank Bond Insti tution Trust Private Trust Taxable

  7. 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?

  8. 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); }

  9. 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

  10. 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); } }

  11. 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

  12. 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?

  13. 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.

  14. 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){ ... } . . . }

  15. 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); }

  16. 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); } }

  17. 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.

  18. 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

  19. 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(); }

  20. 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

  21. 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; } }

  22. 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

  23. 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--); } } } }

  24. 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

  25. Questions? Thank You

More Related