Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 7.

Similar presentations


Presentation on theme: "Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 7."— Presentation transcript:

1 Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 7

2 Rina Zviel-Girshin @ARC2 Overview Object class Final Polymorphism

3 Rina Zviel-Girshin @ARC3 The Object Class Java defines the class java.lang.Object that is defined as a superclass for all classes. All classes directly or indirectly extend the Object class. If a class does not specify explicitly which class it is derived from, then it will be implicitly derived from class Object. All classes inherit Object’s methods.

4 Rina Zviel-Girshin @ARC4 Point ColoredPoint Object Hierarchy Diagram We can depict the relationship between this classes in the following diagram, that is called class hierarchy diagram.

5 Rina Zviel-Girshin @ARC5 The Object Class The following two class definitions are equivalent: class SomeClass { // some code } and class SomeClass extends Object { // some code } Because all classes derived from the Object.

6 Rina Zviel-Girshin @ARC6 Methods of Object Class The Object class defines a set of methods that are inherited by all classes. Some of them are: equals(Object o), getClass(), toString() toString() method that is used whenever we want to get a String representation of an object. When you define a new class, you can override the toString() method in order to have a suitable representation of the new type of objects as Strings.

7 Rina Zviel-Girshin @ARC7 Overriding toString() public class Point { private int x,y; public Point(int x, int y) {this.x=x; this.y=y; } public int getx() {return x;} public int gety() {return y;} public String toString() { return "(" + x + "," + y + ")"; } }

8 Rina Zviel-Girshin @ARC8 Overriding toString() public class ColoredPoint extends Point { private Color color; public ColoredPoint(int x, int y,Color color) { super(x,y); this.color=color; } void setColor(Color color) { this.color = color; } public String toString() { return "(" + getx() + "," + gety() + "," + color + ")"; }

9 Rina Zviel-Girshin @ARC9 Overriding toString() import java.awt.Color; class PrintingPointExample { public static void main(String[] args) { ColoredPoint p = new ColoredPoint(2,3,Color.red); System.out.println(p); }

10 Rina Zviel-Girshin @ARC10 Method equals(Object o) Indicates whether some other object is "equal to" this one. For any reference values x and y, this method returns true if and only if two separate objects x and y refer are of the same type and contain the same data. Note that is is not the same as comparing two object references using the == operator. That test simply determines if two object references point to the same object.

11 Rina Zviel-Girshin @ARC11 Overloading vs. Overriding Overloading deals with multiple methods in the same class with the same name but different signatures. Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature. Overloading lets you define a similar operation in different ways for different data. Overriding lets you define a similar operation in different ways for different object types.

12 Rina Zviel-Girshin @ARC12 Multiple inheritance Multiple inheritance means that a derived class can have more than one parent. Exist in some-object oriented languages, like C++. Java’s approach to inheritance is single inheritance. Only one extends allowed. Usually you can avoid the need for multiple inheritance with good design of the class hierarchy. But there are other solutions in Java (through interfaces).

13 Rina Zviel-Girshin @ARC13 Final modifier final modifier can be used to make class childless. Syntax: final class SubClass extends SuperClass { //…} Means SubClass is the last class in class hierarchy. final methods cannot be redefined. –Allows the virtual machine to optimize the byte code. –For security reasons.

14 Rina Zviel-Girshin @ARC14 Final example class Feline { boolean isHungry = true; void speak() {} void call() { System.out.println("here kitty, kitty..."); if (_isHungry) speak(); } final class Cat extends Feline { String race; void speak() { System.out.println("meow!"); } } final class Lion extends Feline { void speak() { System.out.println("roar!"); } void hunt() {...} } A final class cannot be derived further.

15 Rina Zviel-Girshin @ARC15 Polymorphism The Webster definition: pol-y-mor-phism n. 1. the state or condition of being polymorphous. 2. a. genetic variation that produces differing characteristics in individuals of the same population or species. pol-y-mor-phous adj. 1. having, assuming, or passing through many or various forms, stages, or the like;

16 Rina Zviel-Girshin @ARC16 Polymorphism The basic principle and aims of the generality and abstraction are: reuse interoperability The basic idea: Programmer uses a single, general interface Java selects the correct method. or Write once use anywhere.

17 Rina Zviel-Girshin @ARC17 Polymorphism ColoredPoint is a Point. Point is an Object. ColoredPoint can be used by any code designed to work with Point objects. If a method expects a Point object you can hand it a ColoredPoint object and it will work. This means that ColoredPoint object can be used as both a Point object and a ColoredPoint object (ColoredPoint can have many forms).

18 Rina Zviel-Girshin @ARC18 Polymorphism We can view a RestrictedFile object from 3 different points of views: As a RestrictedFile. This is the most narrow point of view (the most specific). This point of view ‘sees’ the full functionality of the object. As a File. This is a wider point of view (a less specific one). We forget about the special characteristics the object has as a RestrictedFile (we can only open and close the file). As an plain Object.

19 Rina Zviel-Girshin @ARC19 Polymorphism We view an object by using an object reference. A variable of type ‘reference to File’ can only refer to an object which is a File. File f = new File(“story.txt”); But a RestrictedFile is also a File, so f can also refer to a RestrictedFile object. File f = new RestrictedFile(“visa.dat”); The type of the reference we use determines the point of view we will have on the object.

20 Rina Zviel-Girshin @ARC20 Polymorphism If we refer to a RestrictedFile object using a RestrictedFile reference we have the RestrictedFile point of view. We see all the methods that are defined in RestrictedFile and up the hierarchy tree. 4RestrictedFile f = new RestrictedFile(“visa.dat”,12345); 4f.close(); 4f.lock(); 4f.unlock(12345); 4String s = f.toString();

21 Rina Zviel-Girshin @ARC21 Polymorphism If we refer to a RestrictedFile object using a File reference we have the File point of view - which lets us use only methods that are defined in class File and up the hierarchy tree. 4File f = new RestrictedFile (“visa.dat”,12345); 4f.close(); 8f.lock(); 8f.unlock(12345); 4String s = f.toString();

22 Rina Zviel-Girshin @ARC22 Polymorphism If we refer to a RestrictedFile object using an Object reference we have the Object point of view - which let us see only methods that are defined in class Object. 4Object f = new RestrictedFile (“visa.dat”,12345); 8f.close(); 8f.lock(); 8f.unlock(12345); 4String s = f.toString();

23 Rina Zviel-Girshin @ARC23 Polymorphism RestrictedFile ...  isOpen  isLocked  key  toString() ...  isOpen()  open()  close()  lock()  unlock(key)  isLocked()

24 Rina Zviel-Girshin @ARC24 RestrictedFile reference RestrictedFile point of view File reference File point of view Widening Changing our point of view of an object, to a wider one (a less specific one) is called widening. File file = new RestrictedFile((“visa”, 1234);

25 Rina Zviel-Girshin @ARC25 Polymorphism A method is polymorphic if the action performed by the method depends on the actual type of the object to which the method is applied. In OOP calling a method is often referred to as sending a message to an object. The object responds to the message by executing the appropriate method. Polymorphism just means that different objects can respond to the same message in different ways.

26 Rina Zviel-Girshin @ARC26 Polymorphic method The statement System.out.println(point); is a message to the object referred to by point. Since that object knows what type of object it is, it knows how it should respond to the message: If point is a reference to Point then toString method of Point class is invoked. If point is a reference to ColoredPoint then toString method of ColoredPoint class is invoked.

27 Rina Zviel-Girshin @ARC27 Example public String toString() { return "(" + x + "," + y + ")"; } public String toString() { return "(" + getx() + "," + gety() + "," + color + ")"; }

28 Rina Zviel-Girshin @ARC28 Java Methods are Virtual We say that methods in Java are virtual, which means that the type of method which will be invoked depends on the type of object (and not on the type of reference), and this type is determined at runtime. There are languages that use different mechanisms for method invocation.

29 Rina Zviel-Girshin @ARC29 Example class PointExample { public static void main(String[] args) { Point point; if (Math.random() >= 0.5) { point = new Point(3,4);} else { point = new ColoredPoint(5,6,Color.red);} System.out.println(point); } What will be printed if the number tossed is greater than 0.5? (3,4)

30 Rina Zviel-Girshin @ARC30 Any Questions?


Download ppt "Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 7."

Similar presentations


Ads by Google