Presentation is loading. Please wait.

Presentation is loading. Please wait.

OO terminology & objectives §Encapsulation l don’t touch my private data l get/set it using my public/package methods §Inheritance l a parent provides.

Similar presentations


Presentation on theme: "OO terminology & objectives §Encapsulation l don’t touch my private data l get/set it using my public/package methods §Inheritance l a parent provides."— Presentation transcript:

1 OO terminology & objectives §Encapsulation l don’t touch my private data l get/set it using my public/package methods §Inheritance l a parent provides for her/his children l children add to a parent’s knowledge §Polymorphism l Java figures out family relationships: when the parent knows best or when the child knows more than the parent

2 Inheritance in Java usuper class or parent, subclass or child uadds functionality to an existing class  class Child extends Parent uC++ multiple inheritance … NOT! u 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 u “The Ultimate Superclass” - all classes in Java extend the Object class u Example (an array of generic objects): ObjectDemo.java

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

5 Polymorphism uthe same method call on two objects in the same “family” (inheritance branch) can give different results uWhich method is actually called at run time?  method(signature) in this object’s class  else look up the super (parent) class tree urun-time/dynamic binding determines which one ucompile time: the declared type of an object reference Object x = new BigDecimal("123.45"); urun-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's but 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 ulike when your parent said, “…and that’s final!”. ua method of limiting inheritance

7 Casting and Inheritance ucasting up: superclass = subclass; // superclass: the declared type of the variable is the superclass // subclass: the declared type of the variable is the subclass ucasting down: subclass = (subclass) superclass; // a promise to the compiler // the validity of “casting down” is checked at run time ucompile-time error: subclass = superclass; uExamples: Inherit2.java, Inherit3.java

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? uan abstraction(i.e. generalization) of subclasses - a class hierarchy is enforced uabstract methods: the developers of the subclasses MUST customize the code for these methods ucannot be used to create an object instance Aclass aClassRef; // object reference is OK aClassRef = new Aclass(); // but cannot construct uin practice: an abstract class has a mix of instance variables, implemented and abstract methods

11 Interfaces in Java uWhat is an interface? uHow to declare an interface? uHow to implement an interface? uHow to use an interface?

12 What is an interface? usyntax: a declaration of methods (that are not implemented yet) usoftware 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 ucannot be used to create an object ustatic final variables may be included (e.g. the Adjustable interface)

13 How is an interface used? uused as a reference type - Example 1: DigitalPlayer.java, CDPlayer.java, TestPlayer.java - Example 2: FoodProcessor.java - Advantage: the client code will not be broken(i.e. affected) by a different implementation of the interface ue.g. implements Comparable … int compareTo(Object obj) see API for Comparable interface uevent handling(GUI programming) - Example: the ActionListener interface ua class may implement many interfaces ( vs extending just one [abstract] class) uan interface can extend one or more interfaces

14 Inner Classes ua class that is defined inside another class, called the outer or enclosing class. uaccess privilege to members of the outer class: fields, methods, other inner classes ucan access outer class’s private instance members umostly used for event-driven programs i.e. GUI programming uExample: Outer.java & OuterInnerTest.java

15 Anonymous Inner Classes uanonymous inner class definition (i.e. an inner class that does not have a name) - notational convenience: event handling code can be put close to where GUI objects are created

16 Anonymous Inner Classes uan anonymous inner class that implements an interface uan anonymous inner class that extends a class uExample: Anonymous.java

17 Inner Classes uinner classes than implement an interface Example 1: PhotoCopier.java uanonymous inner class as used for GUI Example 2: JFrameDemo.java uRationale: coding convenience


Download ppt "OO terminology & objectives §Encapsulation l don’t touch my private data l get/set it using my public/package methods §Inheritance l a parent provides."

Similar presentations


Ads by Google