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 u "is a" test for inheritance/super class u a String is an Object (true) u an Object is a String (false)

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 is automatic: Object object; String string; object = string; // because string IS AN object ucasting down is up to the programmer: string = (String) object; u promises compiler that, at run time, object will refer to a String type. ucompile-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? 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?  one or more interfaces can be implemented by a class class MyClass implements Comparable  other classes can tell if your class has implemented an interface if (myClass instanceof Comparable){  then other classes know they can call interface methods int compare=myClass.compareTo(otherMyClass)} uonly your class knows how to implement those methods usee API for Comparable interface uevent handling(GUI programming) - Example: the ActionListener interface 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 uan inner class that does not have a name unotational convenience: event handling code can be put close to where GUI objects are created unot recommended because u code is messy and dense u can only implement behaviour, i.e. override methods u must use default constructor, i.e. cannot have state


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