Object Oriented Programming in Java

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

OBJECT ORIENTED PROGRAMMING M Taimoor Khan
Inheritance Inheritance Reserved word protected Reserved word super
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
OOP Languages: Java vs C++
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
Introduction to Object Oriented Programming CMSC 331.
Chapter 12 Support for Object oriented Programming.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Object Oriented Programming
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
CSCE 240 – Intro to Software Engineering Lecture 3.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Lecture 5:Interfaces and Abstract Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming
Data Abstraction: The Walls
Object-Oriented Design
Object-Oriented Programming Concepts
Inheritance ITI1121 Nour El Kadri.
Inheritance and Polymorphism
Chapter 11 Object-Oriented Design
Object-Oriented Programming
Types of Programming Languages
Chapter 12 Abstract Classes and Interfaces
Java Inheritance.
Chapter 8 Class Inheritance and Interfaces
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Object-Oriented PHP (1)
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
C++ Object Oriented 1.
Presentation transcript:

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 new- expression 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