Exercise: Singleton 1 In some situations, you may create the only instance of the class. 1 class mysingleton { 2 3 // Will be ready as soon as the class is loaded. 4 private static mysingleton Instance = new mysingleton(); 5 6 // Do now allow to invoke the constructor by other classes. 7 private mysingleton() {} 8 9 // Only way to obtain the singleton from the outside world. 10 public static mysingleton getsingleton() { 11 return Instance; 12 } 13 } To check the uniqueness, you may declare a static field to count the number of mysingleton objects. 1 See any textbook for design patterns. Zheng-Liang Lu Java Programming 249 / 283
Garbage Collection (GC) 2 Java handles deallocation automatically. Automatic garbage collection is the process of looking at the heap memory, identifying whether or not the objects are in use, and deleting the unreferenced objects. An object is said to be unreferenced if the object is no longer referenced by any part of your program. Simply assign null to the reference-type variable to make the object referenced by this variable unreferenced. So the memory used by an unused object can be reclaimed. You may invoke System.gc() to execute the deallocation procedure. However, frequent invocation of GC is time-consuming. 2 http://www.oracle.com/webfolder/technetwork/tutorials/obe/ java/gc01/index.html Zheng-Liang Lu Java Programming 250 / 283
finalize() Method By writing the finalize() method, you can define specific actions that will be executed while an object is about to be reclaimed by GC. GC runs periodically, checking for objects that are no longer referenced directly or indirectly by other objects. So the finalize() method is only invoked prior to GC. In practice, it must not rely on the finalize() method for normal operations. (Why?) Zheng-Liang Lu Java Programming 251 / 283
Example 1 public class finalizedemo { 2 public void finalize() { 3 System.out.println("Finalizing!"); 4 } 5 6 public static void main(string[] args) { 7 for (int i = 1; i <= 1e7; i++) { 8 new finalizedemo(); 9 } 10 } 11 } You may try different number of the instances. The number of the objects reclaimed by GC is not deterministic. Zheng-Liang Lu Java Programming 252 / 283
HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all objects have their own lifecycle, but there is ownership and child objects can not belong to another parent object. For example, professor department Composition is a specialized form of aggregation and we can call this as a death relationship. For example, house room Zheng-Liang Lu Java Programming 253 / 283
Example: Lines on 2D Cartesian Coordinate +2: two Point objects contained in a Line object. Zheng-Liang Lu Java Programming 254 / 283
More Examples More geometric objects, say Circle, Triangle, and Polygon. Complex number (a + bi) equipped with + and so on. Book with Authors. Lecturer and Students in the classroom. Zoo with many creatures, say Dog, Cat, and Bird. Channels played on TV. Zheng-Liang Lu Java Programming 255 / 283
More Relationships Between Classes So far, we have seen how to define classes and also create objects. Recall that classes would be little more than a convention for organizing code. Moreover, there exist the relationships among classes: inheritance: passing down states and behaviors from parent classes to its child classes interfaces: specifying the methods as an interface to the outside world for classes packages: grouping related types, providing access protection and name space management Zheng-Liang Lu Java Programming 256 / 283
Inheritance Different kinds of objects often share common properties with each other. For example, mountain bikes, road bikes, and tandem bikes all share the characteristics of bicycles (current speed, current pedal cadence, current gear). Yet, each also defines additional features that make themselves more specific. For simplicity, let A be a class defined as follows: 1 class A { 2 int x = 1; 3 void foo() { 4 System.out.println("This is from A."); 5 } 6 } Zheng-Liang Lu Java Programming 257 / 283
Example 1 class B extends A { 2 int y = 2; 3 void hoo() { 4 System.out.println("This is from B."); 5 } 6 } 7 8 public class inheritancedemo { 9 public static void main(string[] args) { 10 A a = new A(); 11 a.foo(); 12 System.out.println("a.x = " + a.x); 13 14 B b = new B(); 15 b.foo(); 16 b.hoo(); 17 System.out.println("b.x = " + b.x); 18 System.out.println("b.y = " + b.y); 19 } 20 } Zheng-Liang Lu Java Programming 258 / 283
Note that a subclass inherits those members from its superclass not designated as private. 1 class A { 2 private int x = 1; 3 private void foo() {... } 4 } 5 6 class B extends A {... } 7 8 public class inheritancemain { 9 public static void main(string[] args) { 10... 11 B b = new B(); 12 b.foo(); // Oops, invisible! 13 b.hoo(); 14 System.out.println("b.x = " + b.x); // Oops, invisible! 15 System.out.println("b.y = " + b.y); 16 } 17 } Zheng-Liang Lu Java Programming 259 / 283
First IS-A Relationship Object-oriented programming allows classes to inherit commonly used states and behaviors from other classes. So classes exist in a hierarchy. A class can be declared as a subclasses of another class, which is called superclass, by using the extends keyword. So we can say that a subclass specializes its superclass. Equivalently, one subclass is a special case of the superclass. Note that a class can extend only one other class. Each superclass has the potential for an unlimited number of subclasses. Zheng-Liang Lu Java Programming 260 / 283
Class Hierarchy 3 3 See Fig. 3-1 in p. 113 of Evans and Flanagan. Zheng-Liang Lu Java Programming 261 / 283
The super Keyword Recall that the keyword this is used to refer to the object itself. You can use the keyword super to refer to members of the superclass. Note that this() and super() are used as the constructor of the current class and that of its superclass, respectively. Make sure all the constructor are invoked in the first line in the constructors. Zheng-Liang Lu Java Programming 262 / 283
Constructor Chaining If a subclass constructor invokes a constructor of its superclass, you might think that there will be a whole chain of constructors called, all the way back to the constructor of the class Object, the topmost class in Java. So every class is an immediate or a distant subclass of Object. Recall that the method finalize() and tostring() are inherited from Object. tostring(): return a string which can be any information stored in the class. Zheng-Liang Lu Java Programming 263 / 283
1 class A { 2 int x; 3 Example 4 A() { 5 System.out.println("A..."); 6 } 7 8 A(int x) { 9 this(); 10 this.x = x; 11 } 12 } 13 14 class B extends A { 15 B() { 16 super(10); 17 System.out.println("B..."); 18 } 19 } 20 21 22 Zheng-Liang Lu Java Programming 264 / 283
23 public class constructorchainingdemo { 24 public static void main(string[] args) { 25 B b = new B(); 26 System.out.println(b.x); 27 } 28 } Zheng-Liang Lu Java Programming 265 / 283
Field Hiding If one field whose name is same to the field inherited from the superclass, then the newly field hides that of the superclass. In other words, the shadowed field of the superclass cannot be referenced by its simple name. Instead, the field must be accessed through the key word super. Zheng-Liang Lu Java Programming 266 / 283
Example 1 class A { 2 int x = 1; 3 } 4 5 class B extends A { 6 int x = 2; 7 8 int getxfroma() { 9 return super.x; 10 } 11 } 12 13 class fieldhidingdemo { 14 public static void main(string[] args) { 15 B b = new b(); 16 System.out.println(b.x); // output 2 17 System.out.println(b.getXfromA()); // output 1 18 } 19 } Zheng-Liang Lu Java Programming 267 / 283
Method Overriding When a class defines an instance method using the same name, return type, and parameters as a method in its superclass, that method overrides the method of the superclass. Key elements: inheritance, exactly same method definition, including the return type Recall that we could declare overloaded methods, that is, the methods with different signatures within a class. Key elements: in the same class, different signatures Similarly if your method overrides one of its superclass s methods, you can invoke the overridden method through the use of the keyword super. Zheng-Liang Lu Java Programming 268 / 283
When there are multiple implementations of the method in the inheritance hierarchy, the one in the most derived class (the furthest down the hierarchy) always overrides the others, even if we refer to the object through a reference variable of the superclass type. 4 4 An overridden method in Java acts like a virtual function in C++. Zheng-Liang Lu Java Programming 269 / 283
Example Zheng-Liang Lu Java Programming 270 / 283
Binding Association of method definition to the method call is known as binding. The binding which can be resolved at compile time by compiler is known as static binding or early binding. They are the static, private and final methods. Compiler knows that all such methods cannot be overridden and will always be accessed by object of local class. When compiler is not able to resolve the binding at compile time, such binding is known as dynamic binding or late binding. For example, method overriding. Zheng-Liang Lu Java Programming 271 / 283
Polymorphism In OO design, the word polymorphism, which literally means many forms, refers to the ability of reference variables to take different forms. The Liskov Substitution Principle states that one variable associated with the declared type can be assigned an instance from any direct or indirect subclass of that type. 5 Let U be a a subtype of T. Then T variables may refer to U objects. 5 It is a semantic rather than merely syntactic relation because it intends to guarantee semantic interoperability of types in a hierarchy, object types in particular. See https://en.wikipedia.org/wiki/liskov_substitution_principle. Zheng-Liang Lu Java Programming 272 / 283
Casting Upcasting (widening conversion) is to cast the U object to the T variable. 1 T t = new U(); Downcasting (narrow conversion) is to cast the T variable to a U variable. 1 U u = (U) t; // t is T variable reference to a U object. Upcasting is always allowed, but downcasting is allowed only when the T variable refer to a real U object. This involves type compatibility by JVM during program execution. Zheng-Liang Lu Java Programming 273 / 283
instanceof Operator The operator instanceof allows us to test whether or not a reference variable is compatible to the object. If not compatible, then JVM will throw an exception ClassCastException. 6 6 We will see the exceptions later. Zheng-Liang Lu Java Programming 274 / 283
1 class T {} 2 3 class U extends T {} 4 Example 5 public class instanceofdemo { 6 public static void main(string[] args) { 7 T t1 = new T(); 8 9 if (t1 instanceof U) 10 System.out.println("t1 is an instance of U."); 11 else 12 System.out.println("t1 is not an instance of U."); 13 14 if (t1 instanceof Object) 15 System.out.println("t1 is an instance of Object."); 16 else 17 System.out.println("t1 is not an instance of Object. "); 18 19 20 21 Zheng-Liang Lu Java Programming 275 / 283
22 23 T t2 = new U(); // Upcasting 24 25 if (t2 instanceof U) 26 System.out.println("t2 is an instance of U."); 27 else 28 System.out.println("t2 is not an instance of U."); 29 30 U u = (U) t2; // Downcasting; this is ok. 31 32 u = (U) new T(); // Oops, this is now allowed. 33 } 34 } Zheng-Liang Lu Java Programming 276 / 283
Exercise Zheng-Liang Lu Java Programming 277 / 283
1 public class animalfamilydemo { 2 public static void main(string[] args) { 3 Dog d = new Dog(); 4 d.sleep(); 5 d.eat(); 6 d.watchdoor(); 7 8 Cat c = new Cat(); 9 c.sleep(); 10 c.eat(); 11 c.chaselittlething(); 12 13 Animal a = d; // Upcasting 14 a.sleep(); 15 a.eat(); 16 a.watchdoor(); // Oops, invisible! 17 a.chaselittlething(); // Oops, invisible! 18 19 d = (Dog) a; // Downcasting 20 d.watchdoor(); // Works! 21 } 22 } Zheng-Liang Lu Java Programming 278 / 283
Field Hiding with Polymorphism You can refer to hidden fields simply by casting an object to a variable of the appropriate superclass. 1 class T { 2 int x = 1; 3 } 4 5 class U extends T { 6 int x = 2; 7 } 8 9 public class fieldhidingdemo { 10 public static void main(string[] args) { 11 U u = new U(); 12 System.out.println(u.x); // output 2 13 T t = u; 14 System.out.println(t.x); // output 1 15 } 16 } Zheng-Liang Lu Java Programming 279 / 283
Method Overriding with Polymorphism However, you cannot invoke overridden methods by upcasting. JVM calls the appropriate method for the object. Method lookup starts from the bottom of the class hierarchy to the top. Always looking for the most specific method body. These methods are referred to as virtual methods. This mechanism preserves the behaviors of the objects and the superclass type variables play the role of placeholders. Zheng-Liang Lu Java Programming 280 / 283
Example Imagine that we have a zoo with some animals. 1 class Animal { 2 String name; 3 void speak() {} 4 } 5 6 class Dog extends Animal { 7 Dog(String name) { 8 this.name = name; 9 } 10 void speak() { 11 System.out.println(name + ": wooooo"); 12 } 13 } 14 15 class Cat extends Animal { 16 Cat(String name) { 17 this.name = name; 18 } 19 void speak() { Zheng-Liang Lu Java Programming 281 / 283
20 System.out.println(name + ": mewwwww"); 21 } 22 } 23 24 class Bird extends Animal { 25 Bird(String name) { 26 this.name = name; 27 } 28 void speak() { 29 System.out.println(name + ": ziiii"); 30 } 31 } 32 33 public class polymorphismdemo { 34 public static void main(string[] args) { 35 Animal[] zoo = new Animal[6]; 36 37 zoo[0] = new Dog("Dog 1"); 38 zoo[1] = new Cat("Cat 1"); 39 zoo[2] = new Cat("Simon"); 40 zoo[3] = new Bird("B1"); 41 zoo[4] = new Bird("B2"); Zheng-Liang Lu Java Programming 282 / 283
42 zoo[5] = new Bird("B3"); 43 44 for (int i = 0; i < zoo.length; i++) { 45 zoo[i].speak(); 46 } 47 } 48 } Zheng-Liang Lu Java Programming 283 / 283