The major elements of the object-oriented model

Similar documents
Chapter 5 Object-Oriented Programming

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

C++ Important Questions with Answers

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

PROGRAMMING LANGUAGE 2

Programming II (CS300)

Java Object Oriented Design. CSC207 Fall 2014

Practice for Chapter 11

Inheritance and Polymorphism

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

Chapter 5. Inheritance

Java Fundamentals (II)

Inheritance and object compatibility

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

PRINCIPLES OF SOFTWARE DESIGN

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Polymorphism and Inheritance

Lecture Notes on Programming Languages

VIRTUAL FUNCTIONS Chapter 10

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

Chapter 14 Abstract Classes and Interfaces

Introduction to Programming Using Java (98-388)

CMSC 132: Object-Oriented Programming II

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

JAVA MOCK TEST JAVA MOCK TEST II

QUESTIONS FOR AVERAGE BLOOMERS

1. Software Systems Complexity, OO Paradigm, UML

Programming II (CS300)

More about inheritance

Lecture 5: Inheritance

What are the characteristics of Object Oriented programming language?

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

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

CS-202 Introduction to Object Oriented Programming

OBJECT ORİENTATİON ENCAPSULATİON

CSC9T4: Object Modelling, principles of OO design and implementation

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Inheritance. Transitivity

BCS Higher Education Qualifications. Diploma in IT. Object Oriented Programming Syllabus

CPS 506 Comparative Programming Languages. Programming Language

[ L5P1] Object-Oriented Programming: Advanced Concepts

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Object-Oriented Concepts and Design Principles

Inheritance, Polymorphism, and Interfaces

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

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

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

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

OVERRIDING. 7/11/2015 Budditha Hettige 82

Object Model. Object Orientated Analysis and Design. Benjamin Kenwright

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

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

15CS45 : OBJECT ORIENTED CONCEPTS

Chapter 10 Classes Continued. Fundamentals of Java

CHAPTER 5 GENERAL OOP CONCEPTS

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Chapter 9 Inheritance

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

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

Concepts of Programming Languages

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

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

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

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

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

Inheritance -- Introduction

Class, Variable, Constructor, Object, Method Questions

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

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

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

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

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

OO Design with Multiple Inheritance

Object Oriented Programming: Based on slides from Skrien Chapter 2

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Microsoft Visual Basic 2005: Reloaded

CS 251 Intermediate Programming Inheritance

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

Check out Polymorphism from SVN. Object & Polymorphism

Data Abstraction. Hwansoo Han

More Relationships Between Classes

Starting Out with Java: From Control Structures Through Objects Sixth Edition

First IS-A Relationship: Inheritance

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

What is Inheritance?

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

What about Object-Oriented Languages?

8. Polymorphism and Inheritance

Java Magistère BFA

Transcription:

The major elements of the object-oriented model Abstraction Encapsulation Inheritance Modularity Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Reusing Classes Hierarchy 2 An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer. An abstraction captures the entire behavior of some object, no more and no less, and offers no surprises or side effects that go beyond the scope of the abstraction. Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation. [G. Booch et al. Object-Oriented Analysis and Design with Applications. 3rd edition, 2007.] [G. Booch et al. Object-Oriented Analysis and Design with Applications. 3rd edition, 2007.] 3 4

Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules. [G. Booch et al. Object-Oriented Analysis and Design with Applications. 3rd edition, 2007.] Hierarchy is a ranking or ordering of abstractions. The two most important hierarchies in a complex system are its class structure (the is a hierarchy) and its object structure (the part of hierarchy). [G. Booch et al. Object-Oriented Analysis and Design with Applications. 3rd edition, 2007.] 5 6 part of 7 8

Is this a part of relation? What about a person that can also think of two numbers? 9 10 is a Copy-paste is dangerous! 11 12

is a Access Specifiers 13 14 The super keyword 1 Initialization Order 2 Output: 15 16

Inheritance and Constructors Inheritance and Constructors Compilation Error!!! public PersonTwo() { super(); } 17 18 Extending...the Object Class 19 20

SubClass is a type of the SuperClass. PersonTwo is a type of Person. Output: 21 22 Identity (memory address) State (given by its attributes) An Object has... Interface (known by its user) Behavior (known by its implementer) Compilation Error!!! 23 24

25 26 Attributes Final Members Entities that cannot be changed Methods Classes 27 Polymorphism Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Polymorphism

USB + transferpctodevice(pc pc, Device device) : void + transferdevicetopc(pc pc, Device device) : void Device - information : String + Device() + Device(info : String) + store(info : String) : void + load() : String PC - memory : String - registry : String + store(information : String) : void + load() : String VideoDevice - producer : String + VideoDevice(info : String, prod : String) + recordmovie() : void PhotoDevice + PhotoDevice(info : String) + takepicture() : void 30 31 32

Overriding Methods 33 34 Overriding Methods A non static method in a subclass with the same signature (same name, same number and type of parameters) and return type as a non static method in the superclass overrides the method from the superclass. Compilation Error!!! 35 36

Specializing Methods Calls the method defined in the Superclass! Output: 37 38 Method-call Binding Polymorphism Connecting a method call to a method body is called binding. Early binding is done before the program is run. Late binding a.k.a. dynamic binding or runtime binding is done at run time, based on the type of the object. [B. Eckel. Thinking in Java. 4th edition. Prentice Hall. 2006] Type Inheritance + Late Binding Polymorphism allows us to use in an uniform manner different types of objects via Type Inheritance and also differently every object type, if necessarily via Late Binding. 39 40

Overloading versus Overriding Overloading versus Overriding public boolean equals(book o) overloading!!! 41 public boolean equals(book o) overloading!!! 42 Overloading versus Overriding Constructors and polymorphic methods supervalue = 0!!! public boolean equals(object o) overriding!!! 43 44

Polymorphic attributes??? static and overriding methods??? 45 Compilation Error!!! overriding method is static 46 static and overridden methods??? Compilation Error!!! overridden method is static 47 48

Open-Closed Principle Use Device!!! Software entities should be open for extension, but closed for modification. [B.Meyer. Object-Oriented Software Construction.1988] [R. Martin. 1996. http://www.objectmentor.com/resources/articles/ocp.pdf] 49 50 Move functionalities in subclasses and use them via polymorphic calls!!! Figure f = new Figure(); not useful!!! Abstract classes 51 52

Abstract classes Abstract methods Figure f = new Figure(); not possible!!! not useful!!! 53 54 Abstract classes and methods An abstract class is a class than cannot be instantiated. An abstract method is a method without implementation. A class can be abstract even if it does not contain any abstract method. If a class contains at least an abstract method, then it should be declared as being an abstract class. A subclass should implement all the abstract methods from its superclass, or should be declared as abstract. Interfaces Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Interfaces 55

Class versus Interface Interfaces!= interface java keyword!!! Type = Interface WHAT functionality an object provides Class = Implementation HOW is executed the source code FootballPlayer may be a class an abstract class or... an interface!!! 57 58 Interfaces All the members of an interface are public. All the attributes of an interface are static final. Compilation Error!!! protected not allowed here An interface can extends interfaces (more than one). An interface can be seen as a pure abstract class. Compilation Error!!! interface methods cannot have body 59 60

Compilation Error!!! attempting to assign weaker access privileges 61 62 63 64

Compilation Error!!! TennisFootballPlayer is not abstract and does not override abstract method playtennis() or!!! 65 66 Interfaces When a class implements an interface, it should implement all the methods from the interface or it should be declared as being abstract. An interface can have an undefined number of implementations. Two distinct classes can implement the same interface. A class can extend only one class and implement an undefined number of interfaces. 67 68

Conflicts Conflicts Compilation Error!!! reference to C is ambiguous Compilation Error!!! return type is not compatible with void 69 70 Abstract Classes versus Interfaces Abstract Classes versus Interfaces Should provide an implementation or become abstract!!! Overriding is not mandatory!!! 71 72

20 Prefer class hierarchies to tagged classes Effective Java Companion slides of Joshua Bloch, Effective Java (Second Edition) 74 Shortcomings of tagged classes... Readability is hampered because of multiple implementations. Memory is increased. Constructors are responsible for adding the flavor. Adding a new flavor implies the change of the class. Tagged classes are verbose, error-prone and inefficient!!! are imitations of a class hierarchy!!! 75 76