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

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

Exercise: Singleton 1

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

Example: Count of Points

Instance Members and Static Members

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

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

CS-202 Introduction to Object Oriented Programming

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

What is Inheritance?

Implements vs. Extends When Defining a Class

INSTRUCTIONS TO CANDIDATES

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

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

Inheritance and Polymorphism

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

Overriding Variables: Shadowing

Java Object Oriented Design. CSC207 Fall 2014

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance. Transitivity

VIRTUAL FUNCTIONS Chapter 10

Inheritance and Polymorphism. CS180 Fall 2007

25. Generic Programming

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

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

Chapter 5 Object-Oriented Programming

COP 3330 Final Exam Review

Exercise. Zheng-Liang Lu Java Programming 273 / 324

CS 251 Intermediate Programming Inheritance

Practice for Chapter 11

Inheritance -- Introduction

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

Programming in C# Inheritance and Polymorphism

Binghamton University. CS-140 Fall Dynamic Types

Inheritance (continued) Inheritance

Inheritance and Polymorphism

CS Programming I: Inheritance

Type Hierarchy. Lecture 6: OOP, autumn 2003

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

OBJECT ORİENTATİON ENCAPSULATİON

Questions Answer Key Questions Answer Key Questions Answer Key

Polymorphism. Agenda

Lecture 2: Java & Javadoc

type conversion polymorphism (intro only) Class class

Object Orientated Programming Details COMP360

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

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

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

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

Arrays Classes & Methods, Inheritance

Inheritance, polymorphism, interfaces

Java Fundamentals (II)

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

Lecture 4: Extending Classes. Concept

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

Inheritance and object compatibility

Inheritance & Polymorphism

What are the characteristics of Object Oriented programming language?

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

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

Object-Oriented Concepts

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

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

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Inheritance and Polymorphism

Operators and Expressions

Object Oriented Java

Polymorphism. return a.doublevalue() + b.doublevalue();

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

C++ Important Questions with Answers

Java: introduction to object-oriented features

Inheritance, Polymorphism, and Interfaces

Lecture Notes Chapter #9_b Inheritance & Polymorphism

1 Shyam sir JAVA Notes

Polymorphism and Inheritance

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

Inheritance Motivation

Classes and Inheritance Extending Classes, Chapter 5.2

CMSC 132: Object-Oriented Programming II

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

8. Polymorphism and Inheritance

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

Full file at Chapter 2 - Inheritance and Exception Handling

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

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

CS111: PROGRAMMING LANGUAGE II

CLASS DESIGN. Objectives MODULE 4

CH. 2 OBJECT-ORIENTED PROGRAMMING

Class, Variable, Constructor, Object, Method Questions

Transcription:

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, knight sword 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 251 / 272

Example: Lines on 2D Cartesian Coordinate +2: two Point objects contained in a Line object. Zheng-Liang Lu Java Programming 252 / 272

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 253 / 272

More Relationships Between Classes So far, we have seen how to define classes and also create objects. There exist more relationships among classes: inheritance: passing down states and behaviors from parent classes to its child classes interfaces: grouping the methods, which belongs to some classes, as an interface to the outside world packages: grouping related types, providing access protection and name space management Zheng-Liang Lu Java Programming 254 / 272

Inheritance Different kinds of objects often share common properties with each other. For example, human-beings and dogs are two specific types of animals. 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 255 / 272

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 256 / 272

First IS-A Relationship OO allows classes to inherit commonly used states and behaviors from other classes. So the classes exist in some 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 257 / 272

Class Hierarchy 1 1 See Fig. 3-1 in p. 113 of Evans and Flanagan. Zheng-Liang Lu Java Programming 258 / 272

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 259 / 272

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 260 / 272

1 class A { 2 private 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 public String tostring() { return new String(super.x); } 21 } 22 Zheng-Liang Lu Java Programming 261 / 272

23 24 25 public class constructorchainingdemo { 26 public static void main(string[] args) { 27 B b = new B(); 28 System.out.println(b); 29 } 30 } The println() method takes an object as inputs, and invokes tostring() method implicitly. Zheng-Liang Lu Java Programming 262 / 272

Field Hiding If one field whose name is identical 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 the field name. Instead, the field must be accessed through the key word super. Zheng-Liang Lu Java Programming 263 / 272

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 264 / 272

Method Overriding The subclass is allowed to change the behavior inherited from its superclass if needed. As a subclass defines an instance method using the method name, return type, and parameters all identical to the method of its superclass, this method overrides the one of the superclass. 2 Compared to overridden methods, method overloading occurs only in the same class. Similarly if your method overrides one of its superclass s methods, you can invoke the overridden method through the use of the keyword super. 2 The static methods do not follow this rule. Zheng-Liang Lu Java Programming 265 / 272

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. 3 3 An overridden method in Java acts like a virtual function in C++. See the virtual method table. Zheng-Liang Lu Java Programming 266 / 272

Example Zheng-Liang Lu Java Programming 267 / 272

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 268 / 272

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. 4 Let U be a a subtype of T. Then T variables may refer to U objects. 4 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 269 / 272

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 270 / 272

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. 5 5 We will see the exceptions later. Zheng-Liang Lu Java Programming 271 / 272

1 class T {} 2 class U extends T {} 3 Example 4 public class InstanceofDemo { 5 public static void main(string[] args) { 6 T t1 = new T(); 7 8 System.out.println(t1 instanceof U); // output false 9 System.out.println(t1 instanceof T); // output true 10 11 T t2 = new U(); // upcasting 12 13 System.out.println(t2 instanceof U); // output true 14 System.out.println(t2 instanceof T); // output true 15 16 U u = (U) t2; // downcasting; this is ok. 17 18 u = (U) new T(); // pass the compilation; fail during execution! 19 } 20 } Zheng-Liang Lu Java Programming 272 / 272