Chapter 2: Java OO II X I A N G Z H A N G

Similar documents
Compaq Interview Questions And Answers

Object Oriented Programming. Java-Lecture 11 Polymorphism

Programming II (CS300)

PROGRAMMING LANGUAGE 2

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

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

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Inheritance -- Introduction

OBJECT ORİENTATİON ENCAPSULATİON

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

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

Chapter 5 Object-Oriented Programming

More Relationships Between Classes

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

Practice for Chapter 11

Chapter 14 Abstract Classes and Interfaces

Programming II (CS300)

Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview

Inheritance. Transitivity

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

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 6

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

Polymorphism. Zimmer CSCI 330

More on Objects in JAVA TM

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Java 8 Programming for OO Experienced Developers

Exercise: Singleton 1

CS-202 Introduction to Object Oriented Programming

First IS-A Relationship: Inheritance

Inheritance and Polymorphism

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Java Object Oriented Design. CSC207 Fall 2014

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

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

1 Shyam sir JAVA Notes

Introduction to Object-Oriented Programming

VIRTUAL FUNCTIONS Chapter 10

CMSC 132: Object-Oriented Programming II

Example: Count of Points

Implements vs. Extends When Defining a Class

CS313D: ADVANCED PROGRAMMING LANGUAGE

Class, Variable, Constructor, Object, Method Questions

Basic Principles of OO. Example: Ice/Water Dispenser. Systems Thinking. Interfaces: Describing Behavior. People's Roles wrt Systems

Object Oriented Programming: Based on slides from Skrien Chapter 2

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

Chapter 2: Java OO II. Yang Wang wyang AT njnet.edu.cn

Polymorphism and Interfaces. CGS 3416 Spring 2018

CS313D: ADVANCED PROGRAMMING LANGUAGE

Programming Exercise 14: Inheritance and Polymorphism

1 OBJECT-ORIENTED PROGRAMMING 1

25. Generic Programming

C++ Programming: Polymorphism

[ L5P1] Object-Oriented Programming: Advanced Concepts

Example: Count of Points

Rules and syntax for inheritance. The boring stuff

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

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

Concepts of Programming Languages

Inheritance (Outsource: )

Type Hierarchy. Lecture 6: OOP, autumn 2003

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

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Data Structures (list, dictionary, tuples, sets, strings)

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Inheritance and Polymorphism

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

What is Inheritance?

Object-Oriented Programming

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Inheritance and object compatibility

ITI Introduction to Computing II

Inheritance and Interfaces

Inheritance, Polymorphism, and Interfaces

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

Inheritance and Polymorphism

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

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Course Description. Learn To: : Intro to JAVA SE7 and Programming using JAVA SE7. Course Outline ::

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Inheritance and Polymorphism. CS180 Fall 2007

Certification In Java Language Course Course Content

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Comp 249 Programming Methodology Chapter 8 - Polymorphism

Week 5-1: ADT Design

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

ITI Introduction to Computing II

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

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Programming in C# Inheritance and Polymorphism

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Objects and Iterators

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

Transcription:

Chapter 2: Java OO II X I A N G Z H A N G j a v a c o s e @ q q. c o m

Content 2 Abstraction Abstract Class Interface Inheritance Polymorphism

Abstraction

What is Abstraction? Abstraction 4 An abstraction is a general idea rather than one relating to a particular object, person, or situation. - From Collins The Significance of Abstraction Model Implementation Language Tools for Abstraction in Java Abstract Class Interface

Abstraction 5

Abstraction Abstract Class Abstract Class:Java Class providing part of implementations Including abstract methods Cannot be used to create objects Must be inherited and implemented 6

Abstraction Abstract Class An Abstract Class for Benchmark A Completed, Specified Class for Benchmark

Lab Work A benchmark for sorting algorithms Input: an Integer array to be sorted Integer[] input = new Integer[1000] Functions of abstract class generating an input array; measuring the time efficiency of sorting; judging the correctness of sorting; 8 Arrays.sort() 2009

Abstraction Interface The language basis of Java is Class But the basis of OOD is Type Class Type + Implementation Java prompts Interface-based OOD Designer only cares of design, or saying, interface; Developer cares of implementation, or saying, classes. 9

Most Interfaces do: Abstraction Interface Distinguish one type with other types Showing that one type can have the ability to do sth: Cloneable:an object of this type can be cloned Comparable: objects of this type can be compared Runnable: an object of this type can be run in a thread Serializable: an object of this type can be serialized 10

Defining an Interface Abstraction Interface Constant Method Nested Class and Interface 11

Abstraction - Interface 12 Interface Is a contract between designer and programmer; Programmer must fulfill the interface with the appearance of a type; Designer doesn t care about anything of the inner implementation

Abstraction Interface 13

Fields in Interface public static Final Must be initialized Abstraction Interface 14

Abstraction Interface 15 Implementing an Interface A Class can implement multiple interfaces Multiple Implementation All methods in an interface should be implemented Multiple Implementation!= Multiple Inheritance

Abstraction Interface 16 Think Does M-Implementation bring the same problem as M- Inheritance?

Abstraction Interface Using Interface to declare the type of objects 17

Abstraction Interface 18 Interface-based Programming Agile: First, lets care about the design, the type, the interface, and ignore the implementation, the class. Reliable: assure the correctness based on the use of types.

Marker Interface Abstraction Interface 19 Nothing defined in an interface It is just a marker Such as Cloneable

Abstraction Interface 20 Abstract Class vs. Interface Multiple inheritance is allowed for Interface, not for abstract class. Abstract class provides part of implementation, while interface has no implementation.

Abstract Interface Combining Abstract Class and Interface 21

Inheritance

Inheritance 23 The Significance of Inheritance Code reuse Enhancement of maintainability Enhancement of scalability Types of Inheritance Inheritance of class Inheritance of interface

Example - House Inheritance Class 24

Inheritance Class 25 Example - GeorgianHouse

Inheritance Class 26

Inheritance Class 27 Constructions in Inheritance 1. Constructors in subclasses should invoke constructors in superclass explicitly for initialization. 2. If step 1 is not satisfied, the default constructor in superclass will be invoked. 3. If no default constructor, but non-default constructor is defined in superclass, step 1 must be satisfied. 4. The invocation of constructors in superclass should be placed foremost.

Thinking in Java Inheritance - Class 28 A robust programming language should have a sound initialization process. Each field in a class should be initialized.

Overriding( 覆盖 ) Inheritance Class 29 Overriding NOT Overloading( 重载 )//what is overloading Overriding means a method in subclass will replace a method with same signature in superclass //what is signature Overriding let the subclass perform differently with superclass

Inheritance Class 30 Example

Inheritance Want to invoke super method? 31 Is it possible to invoke super method by casting? NO The only way is to use in Son 2009

Limits of Overriding Inheritance Class 32 The access rights should be enlarged or unchanged, not be reduced. // why? The return type should be reduced or unchanged, not be enlarged. // why? If return type in superclass is Class A, the return type in sublcass should A If return type in superclass is a primary type, it should be unchanged in subclass.

Thinking in Java Inheritance -Class 33 Overriding MUST retain compatibility (not breaking the behavior specification in superclass) Think House spark_house = new GeorgianHouse(); // Right? GeorgianHouse spark_house = new House(); //Right?

Inheritance Class 34 Examples

Keyword: super Inheritance Class 35

Inheritance Class 36 Hiding

Guess 37 A: GeorgianHouse House The extended class: GeoriganHouse The extended class: GeoriganHouse B: GeorgianHouse GeorgianHouse The extended class: GeoriganHouse The extended class: GeoriganHouse C: GeorgianHouse House The extended class: GeoriganHouse The super class: House D: GeorgianHouse House The extended class: GeoriganHouse The extended class: House for Field, look at the declaration type; for Method, look at the run-time type;

Conversion of Types Inheritance Class Objective:convert an object of one class to another For example: a Parrot to a Bird, ghouse to House 38 Classification of Conversion Upcasting 上溯造型 Downcasting 下溯造型

Inheritance Class 39 Upcasting Upcasting is safe and unrestricted Downcasting : reverse of Upcasting, may be unsafe may cause casting exceptions Use instanceof to check the safety

Self-study RTTI:Run-Time Type Identification Understand RTTI can help you understand Type conversion in Java Polymorphism Reflection in J2EE 40 Read:Think in Java Chapter 10

Keyword: final Inheritance Class final before a class means it is not inheritable final before a method means it is not overridable 41

Self-study Class Inheritance:How and when? Is-a (Inheritance) Has-a(Composition) How to design an extensible class Read: 42 Java Programming Language 3.11 和 3.12 Thinking in Java chapter 6

Inheritance Interface Multiple Inheritance for Interfaces 43 Inheritance of Interfaces also has: Hiding of fields Override of Methods

Polymorphism

Polymorphism 45 Definition of Polymorphism Greek, means Multiple Forms Refers to the existence of different methods with same names Intuition: in OOD, same objects have the same behavior (method name), but have different way of behaving Question: What will the exact behaving result be for a certain object? Significance: Improve the flexibility and versatility in OOP Types of Polymorphism Static polymorphism - polymorphism in compile-time Dynamic polymorphism - polymorphism in run-time

Static Polymorphism Polymorphism 46 Polymorphism which can be determined in compilation Overloading

Dynamic Polymorphism Polymorphism 47 Behavior and result be can only determined in run-time Dynamic Binding The binding of method invocation and method body in run-time Classification of Dynamic Polymorphism Inheritance Polymorphism Interface Polymorphism

Polymorphism 48

Polymorphism 49 Inheritance Polymorphism

Polymorphism Interface Polymorphism 50

Forecast 51 A Notion of Exception Java Exceptions Exception Handling User-defined Exceptions How to Use Exception