310 likes | 331 Views
System development with Java. Instructors: Rina Zviel-Girshin Lecture 7. Overview. Object class Final Polymorphism. 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.
E N D
System development with Java Instructors: Rina Zviel-Girshin Lecture 7 Rina Zviel-Girshin @ARC
Overview • Object class • Final • Polymorphism Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
Hierarchy Diagram Object • We can depict the relationship between this classes in the following diagram, that is called class hierarchy diagram. Point ColoredPoint Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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 + ")"; } } Rina Zviel-Girshin @ARC
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 + ")"; } } Rina Zviel-Girshin @ARC
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); } } Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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). Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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; Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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). Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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. • RestrictedFile f = new RestrictedFile(“visa.dat”,12345); • f.close(); • f.lock(); • f.unlock(12345); • String s = f.toString(); Rina Zviel-Girshin @ARC
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. • File f = new RestrictedFile (“visa.dat”,12345); • f.close(); • f.lock(); • f.unlock(12345); • String s = f.toString(); Rina Zviel-Girshin @ARC
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. • Object f = new RestrictedFile (“visa.dat”,12345); • f.close(); • f.lock(); • f.unlock(12345); • String s = f.toString(); Rina Zviel-Girshin @ARC
Polymorphism RestrictedFile • toString() • ... • isOpen() • open() • close() • lock() • unlock(key) • isLocked() • ... • isOpen • isLocked • key Rina Zviel-Girshin @ARC
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); RestrictedFile referenceRestrictedFile point of view File referenceFile point of view Widening Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
Example public String toString() { return "(" + x + "," + y + ")"; } public String toString() { return "(" + getx() + "," + gety() + "," + color + ")"; } Rina Zviel-Girshin @ARC
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. Rina Zviel-Girshin @ARC
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); } } Example What will be printed if the number tossed is greater than 0.5? (3,4) Rina Zviel-Girshin @ARC
Any Questions? Rina Zviel-Girshin @ARC