Exercise: Singleton 1

Similar documents
HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Instance Members and Static Members

Example: Count of Points

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Example: Count of Points

More Relationships Between Classes

First IS-A Relationship: Inheritance

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

Subtype Polymorphism

Exercise. Zheng-Liang Lu Java Programming 273 / 324

Java Programming. U Hou Lok. Java Aug., Department of Computer Science and Information Engineering, National Taiwan University

Example: Fibonacci Numbers

Java Programming 2. Zheng-Liang Lu. Java2 304 Fall Department of Computer Science & Information Engineering National Taiwan University

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

What is Inheritance?

CS-202 Introduction to Object Oriented Programming

What are the characteristics of Object Oriented programming language?

Implements vs. Extends When Defining a Class

INSTRUCTIONS TO CANDIDATES

Lecture 2: Java & Javadoc

Recursion 1. Recursion is the process of defining something in terms of itself.

Java Object Oriented Design. CSC207 Fall 2014

VIRTUAL FUNCTIONS Chapter 10

Compaq Interview Questions And Answers

Chapter 5 Object-Oriented Programming

Inheritance. Unit 8. Summary. 8.1 Inheritance. 8.2 Inheritance: example. Inheritance Overriding of methods and polymorphism The class Object

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Computer Science II (20073) Week 1: Review and Inheritance

Inheritance and Polymorphism

Inheritance -- Introduction

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Java Programming 2. Zheng-Liang Lu. Java2 306 Fall Department of Computer Science & Information Engineering National Taiwan University

OBJECT ORİENTATİON ENCAPSULATİON

Lecture 4: Extending Classes. Concept

CMSC 132: Object-Oriented Programming II

Type Hierarchy. Lecture 6: OOP, autumn 2003

Inheritance and Polymorphism

Programming in C# Inheritance and Polymorphism

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Inheritance Motivation

Inheritance and Polymorphism. CS180 Fall 2007

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

25. Generic Programming

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

CS Programming I: Inheritance

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

type conversion polymorphism (intro only) Class class

The return Statement

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

COP 3330 Final Exam Review

Class, Variable, Constructor, Object, Method Questions

Practice for Chapter 11

CS313D: ADVANCED PROGRAMMING LANGUAGE

Binghamton University. CS-140 Fall Dynamic Types

Object Oriented Programming. Java-Lecture 11 Polymorphism

Programming in the Large II: Objects and Classes (Part 2)

ITI Introduction to Computing II

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

Inheritance. Transitivity

Lecture 6 Introduction to Objects and Classes

Create a Java project named week10

Inheritance. The Java Platform Class Hierarchy

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Classes and Inheritance Extending Classes, Chapter 5.2

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CSE 401/M501 Compilers

More On inheritance. What you can do in subclass regarding methods:

Overriding Variables: Shadowing

C10: Garbage Collection and Constructors

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

ITI Introduction to Computing II

Inheritance, Polymorphism, and Interfaces

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

CMSC 132: Object-Oriented Programming II

Inheritance, polymorphism, interfaces

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Polymorphism. Final Exam. November 26, Method Overloading. Quick Review of Last Lecture. Overriding Methods.

CH. 2 OBJECT-ORIENTED PROGRAMMING

C++ Programming: Polymorphism

PROGRAMMING LANGUAGE 2

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Subclasses, Superclasses, and Inheritance

Object-Oriented Concepts

Inheritance (continued) Inheritance

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Introductory Programming Inheritance, sections

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Written by John Bell for CS 342, Spring 2018

CSA 1019 Imperative and OO Programming

Questions Answer Key Questions Answer Key Questions Answer Key

CS313D: ADVANCED PROGRAMMING LANGUAGE

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Transcription:

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