1 / 15

OO terminology & objectives

OO terminology & objectives. Encapsulation don’t touch my private data get/set it using my public/package methods Inheritance a parent provides for her/his children children add to a parent’s knowledge Polymorphism

jstorm
Download Presentation

OO terminology & objectives

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. OO terminology & objectives • Encapsulation • don’t touch my private data • get/set it using my public/package methods • Inheritance • a parent provides for her/his children • children add to a parent’s knowledge • Polymorphism • Java figures out family relationships:when the parent knows best or when the child knows more than the parent

  2. Inheritance in Java • super class or parent, subclass or child • adds functionality to an existing class • class Child extends Parent • C++ multiple inheritance … NOT! • single parent families only in Java • use of the keyword super • not this object’s constructor/variable/method,but the one from the super class.

  3. The Object Class • “The Ultimate Superclass” - all classes in Java extend the Object class • Example (an array of generic objects): ObjectDemo.java • "is a" test for inheritance/super class • a String is an Object (true) • an Object is a String (false)

  4. How to override a parent’s method in the child class? • use the same … • method name • signature (parameter list) • return type • different code in the child’s method overrides parent's logic • e.g. equals(), toString() • access level can be increased (e.g. private to public) • a different signature: overloads the method • a different return type: compiler error • Example: Inherit.java, ShapeTest & docs

  5. Polymorphism • the same method call on two objects in the same “family” (inheritance branch) can give different results • Which method is actually called at run time? • method(signature) in this object’s class • else look up the super (parent) class tree • run-time/dynamic binding determines which one • compile time: the declared type of an object referenceObject x = new BigDecimal("123.45"); • run-time: the actual type of the object referred to determines behaviour. It can be the declared class type or one of its sub classes (inheritance).x.toString() calls BigDecimal class method, not Object'sbut it compiles because Object x has toString().

  6. Polymorphism • a final method: dynamic binding is turned off • sub (child) classes can inherit but not override any final super (parent) method • like when your parent said, “…and that’s final!”. • a method of limiting inheritance

  7. Casting and Inheritance • casting up is automatic:Objectobject; String string;object = string; // because string IS AN object • casting down is up to the programmer:string = (String) object; • promises compiler that, at run time, object will refer to a String type. • compile-time error: subclass = superclass;

  8. Abstract Class: An Example public abstract class Aclass { private int m; // instance variable to be inherited // implemented and to be inherited public void set( int t ) { m = t; } // abstract methods declared, not implemented // must be defined by extending class public abstract void show(); public abstract void change(); }

  9. The Subclasses of an Abstract Class public class Bclass extends Aclass { … // inherit Class Aclass’s set method public void show ( ) { … } // implement details public void change ( ) { … } // implement details } public class Cclass extends Aclass { … // inherit Class Aclass’s set method public void show( ) { … } // class C version public void change ( ) { … } // class C version }

  10. What is an abstract class? • an abstraction(i.e. generalization) of subclasses - a class hierarchy is enforced • abstract methods: the developers of the subclasses MUST customize the code for these methods • cannot be used to create an object instanceAclass aClassRef; // object reference is OK aClassRef = new Aclass(); // but cannot construct • in practice: an abstract class has a mix of instance variables, implemented and abstract methods

  11. Interfaces in Java • What is an interface? • How to declare an interface? • How to implement an interface? • How to use an interface?

  12. What is an interface? • syntax: a declaration of methods (that are not implemented yet) • software development viewpoint - an agreement on how a “specific” object can be used by other objects - a promise to other software developers that all the methods will be implemented by a class • cannot be used to create an object • static final variables may be included (e.g. the Adjustable interface)

  13. How is an interface used? • one or more interfaces can be implemented by a classclass MyClass implements Comparable<MyClass> • other classes can tell if your class has implemented an interfaceif (myClass instanceof Comparable){ • then other classes know they can call interface methodsint compare=myClass.compareTo(otherMyClass)} • only your class knows how to implement those methods • see API for Comparable interface • event handling(GUI programming) - Example: the ActionListener interface • an interface can extend one or more interfaces

  14. Inner Classes • a class that is defined inside another class, called the outer or enclosing class. • access privilege to members of the outer class: fields, methods, other inner classes • can access outer class’s private instance members • mostly used for event-driven programsi.e. GUI programming • Example: Outer.java & OuterInnerTest.java

  15. Anonymous Inner Classes • anonymous inner class definition • an inner class that does not have a name • notational convenience: event handling code can be put close to where GUI objects are created • not recommended because • code is messy and dense • can only implement behaviour, i.e. override methods • must use default constructor, i.e. cannot have state

More Related