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

Similar documents
CMSC 132: Object-Oriented Programming II

Data Abstraction. Hwansoo Han

CPS 506 Comparative Programming Languages. Programming Language

CMSC 132: Object-Oriented Programming II

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

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

Programming II (CS300)

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

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

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

Inheritance and Polymorphism

Java Object Oriented Design. CSC207 Fall 2014

What are the characteristics of Object Oriented programming language?

Lecture Notes on Programming Languages

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

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Inheritance. Transitivity

Programming II (CS300)

Object Oriented Programming. Solved MCQs - Part 2

Chapter 10 Classes Continued. Fundamentals of Java

Chapter 4.!Data Abstraction: The Walls! 2011 Pearson Addison-Wesley. All rights reserved 4-1

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

CS 251 Intermediate Programming Inheritance

CS-202 Introduction to Object Oriented Programming

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

Java Magistère BFA

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

Programming I. Course 9 Introduction to programming

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

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

Inheritance, Polymorphism, and Interfaces

Data Abstraction: The Walls

PROGRAMMING LANGUAGE 2

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

Inheritance, polymorphism, interfaces

Chapter 5 Object-Oriented Programming

CSC9T4: Object Modelling, principles of OO design and implementation

Introduction to Inheritance

Inheritance and Interfaces

Chapter 6 Introduction to Defining Classes

Chapter 13 Abstract Classes and Interfaces

CS304 Object Oriented Programming Final Term

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

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

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

ITI Introduction to Computing II

Inheritance -- Introduction

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

G Programming Languages - Fall 2012

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Advanced Placement Computer Science. Inheritance and Polymorphism

C++ Important Questions with Answers

Object oriented programming Concepts

Object-Oriented Programming

Inheritance and Encapsulation. Amit Gupta

Practice for Chapter 11

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

Inheritance, and Polymorphism.

WA1278 Introduction to Java Using Eclipse

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

Object-Oriented Concept

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

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

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

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

Programming, numerics and optimization

ITI Introduction to Computing II

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Reusing Classes. Hendrik Speleers

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

ITI Introduction to Computing II

index.pdf January 21,

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

Get Unique study materials from

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

ITI Introduction to Computing II

Subtyping (Dynamic Polymorphism)

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CSE 307: Principles of Programming Languages

CISC370: Inheritance

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Object Oriented Paradigm

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

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

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

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

Lecture 5: Inheritance

What is Inheritance?

Framework Fundamentals

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

Motivations. Chapter 13 Abstract Classes and Interfaces

Polymorphism Part 1 1

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

Transcription:

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

Motivation for Object Oriented Programming Decrease complexity (use layers of abstraction, interfaces, modularity,...) Reuse existing code, avoid duplication of code Support formal contracting between independent development teams Detect errors as early as possible (general goal of software engineering)

Motivation Object oriented approach was introduced on 1980-s to reduce complexity of programming large software systems (e.g. graphical user interfaces). Flat library of standard functions (common for early imperative programming languages) is not flexible enough to create complex software systems. Powerful and well organized object oriented framework makes programming easier programmer re-uses existing codebase and specifies only these properties/functions she needs to elaborate/change (and framework adjusts to these changes).

Object Object is characterized by State (defined by values of instance variables in Java) Behaviour (defined by instance methods in Java) Identity (defined by memory location in Java) Object = Instance = Specimen =... Instance variable (Java terminology) = (Object) field = Property = Attribute =... Method = Subroutine = Function / Procedure =...

Object Encapsulation data and operations on the data are integrated into whole (object = capsule) ADT approach set of operations is a part of data type Data hiding object state can be changed only by dedicated (usually public) methods - instance variables should be protected from direct modification Object is an instance of the class. E.g. Rex is a dog.

Class Class defines common features of its objects ( template ). E.g. All dogs have a name. Instantiation creating a new object of the class. Subclass can be derived from the class subclass inherits all the features of its parent class. Subclass allows to add new (specific) features and redefine (=override) inherited features. E.g. Dog is (a special kind of) Animal. If A is a subclass of B then B is superclass of A.

Class Hierarchy Generalization common features of similar classes are described on the level of superclass (mental process design the hierarchy of classes). Specialization subclass is created to concretize (refine) certain general features and add specific data/operations to the subclass (process of coding).

Instance Methods and Class Methods Instance methods define the behaviour of an object (=instance). s.length() - the length of string s in Java. Class methods can be used without creating an object (imperative style). Math.sqrt(2.) - square root of 2. Keyword static in Java is used to define class methods.

Instance Variables and Class Variables Instance variables define the state of an object. Each object has individual values of instance variables. In Java, keyword private is appropriate. a.length the length of an array a. Class variables are common (global variables in class scope). Single value is shared between all objects. Keyword static in Java is used. Math.PI constant Pi in Java.

Message Passing Objects communicate in OOP system by sending messages. Message, sent to an object, is interpreted by the object and causes appropriate instance method to be executed. OOP systems may support: Early binding method to be executed when a message is received is known at compile time (from the program text, statically). Late binding ( true OOP systems) method to be executed is chosen dynamically (decision depends on runtime type of the receiver object).

Polymorphism Same notation has different meaning in different contexts Two types of polymorphism: Overloading operation is redefined in subclass and is binded to the activating message statically (compile time choice). Java constructors support overloading. Overriding operation is redefined in subclass and is binded to the activating message dynamically (runtime choice). Java instance methods support overriding.

Inheritance Subclass inherits all the variables and methods of its parent class (if it is not explicitly forbidden). Single inheritance up to one superclass is allowed for each class. Class hierarhy is a tree structure. In Java, class Object is the root class. Multiple inheritance a class may have more than one superclass. If several parents have the same property defined, it may not be clear, which one is inherited (the so-called diamond dilemma ).

Is-a vs Is-able-to and Has-a Relations Inheritance relation means is a kind of. It is possible to model the multiple inheritance using is able to and has a relations. Java supports only single inheritance for classes, but allows a class to implement several interfaces ( is able to contracts).

Constructors Constructor is a special class method to create a new object. Memory for the object is allocated dynamically. E.g. new Integer(6) - returns a new object of type Integer in Java (object is represented by memory location). Calendar.getInstance() - returns a new object of type Calendar in Java (the so-called factory method).

Names in Constructors Constructor name = Class name, if Java newexpression is used. Keyword this inside constructor is used to call another constructor of the same class (with different signature), constructor overloading. Keyword super is used to call a constructor of the superclass.

Destructors Destructor is used to destroy the object and free the memory. There are no implicit destructors in Java garbage collection is used instead. Garbage collection is hidden, sometimes it is possible to make a suggestion to clean up.

Abstract Features It is reasonable to define common features of similar classes in superclass (to reduce the duplication of code). But... sometimes it is impossible to implement these features in superclass. E.g. circle, square, triangle,... are figures and have an area as a common feature. But for each figure the method to calculate its area is different. It is said that area is an abstract feature of a figure and its implementation is delegated to corresponding subclasses.

Abstract Classes and Interfaces Abstract class has some abstract features that are not implemented. It is impossible to create instances of abstract classes. The role of an abstract class is to be a parent class for its subclasses. Interface is a pure abstract class without any implementation. It serves as a contract between programmers certain class implements an interface, if it defines all the methods listed in interface description. Functional interface introduced in Java 8 has one method that can be described using functional style.

Keywords extends and implements Class hierarchy in Java class A extends B implements C, D {... } Class A is a subclass of B (inheritance applies) and also it provides all the methods listed in interfaces C and D (no diamond dilemma here). If extends-part is missing, a class is a subclass of class Object

Object Identity and Equality Object identity in Java is defined by the memory location returned by the new-expression that allocates memory for the object and activates corresponding constructor. Object is represented by this address, there is no difference between reference and object in Java. o1==o2 tests object identity (it means o1 is the same object as o2 ). To test the object equality, use o1.equals(o2) ( o1 is equal to o2 ).

Sending Messages Messages to objects are sent using dot-operator: // create o1 Object o1 = new Object(); // send message tostring() to the object o1 String s1 = o1.tostring(); Class methods use the same syntax, but message is sent to the class: double s2 = Math.sqrt (2.0);

Receiver of the Message In method body the receiver object is referred as this. If message is sent without indicating the receiver, this object (in case of instance method) or current class (in case of class method) is assumed. If we need to call a superclass method, we use keyword super as the receiver.

Overriding Methods are binded to messages during runtime in Java (late binding). To change the behaviour of Java framework, a programmer can redefine (override) methods in subclass. If an object receives a message, system has to choose appropriate method to be executed. Search is performed bottom-up, starting from the most specific class (runtime class of the object), then its superclass, etc. up to the root class Object.

Important Object Methods Important methods to override: tostring textual representation of an object, equals predicate to decide, whether two objects are equal, important with testing frameworks like JUnit clone create a clone of an object (different identity, but equal content) Deep clone vs. shallow clone. hashcode influences behaviour of certain collections (like HashMap)

Interfaces Generic built-in methods will work for our class, if we implement corresponding interface. Example. public int compareto (Object o) is a method in Comparable interface. Making a class Comparable (by implementing compareto) gives a lot of API methods for free, e.g. sort, max, min,... o1.compareto(o2) < 0, if o1 is less than o2 == 0, if o1 equals to o2 > 0, if o1 is greater than o2

Examples Pets.java Phones.java Num.java Complex.java