Object-Oriented Programming Concept. Concepts of Object Orientation Objects and classes. method Message passing Inheritance.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object-oriented Programming Concepts
Visual Basic Introduction IDS 306 from Shelly, Cashman & Repede Microsoft Visual Basic 5: Complete Concepts and Techniques.
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
CPT 140 Programming Constructs1 OBJECT ORIENTED TECHNOLOGY Terminology and Basic Concepts.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance using Java
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
BCS 2143 Introduction to Object Oriented and Software Development.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Introduction to Objects Adapted from “TEN STEPS TO OBJECT-SPEAK” a CPT Tech Talk by Joy Starks September 17, 1999.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Topic 4 Inheritance.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Inheritance A Review of Objects, Classes, and Subclasses.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Coming up: Inheritance
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Object-Oriented Programming: Inheritance and Polymorphism.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Object-Oriented Programming Concepts
Final and Abstract Classes
Inheritance and Polymorphism
Lecture 22 Inheritance Richard Gesick.
Inheritance Inheritance is a fundamental Object Oriented concept
Object-Oriented Programming: Inheritance and Polymorphism
CIS 199 Final Review.
Object Oriented Analysis and Design
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Object-Oriented Programming Concept

Concepts of Object Orientation Objects and classes. method Message passing Inheritance

What is an object? An object is.. - anything real or abstract that store data and operations that manipulate the data. -a software bundle of variables and related methods (operations). -anything that supports the concept of a name. -an instance of a class.

Messages Software objects interact and communicate with each other using messages Bank Object Customer object Account object

Using Object Objects give you two ways to do these things: 1.Manipulate or inspect its variables. 2.Call its methods.

Referencing an Object's Variables objectReference.variableName Eg. int height = new Rectangle().height; Calling an Object's Methods objectReference.methodName(argumentList); or objectReference.methodName(); Eg. rect_two.move(40, 72); int areaOfRectangle = new Rectangle(100, 50).area();

Classes A class defines the attributes and operations common to all objects of a certain kind. A class is an object or a set of objects that share a common structure and a common behavior. A class is created when objects are used repeatedly.

Each class may have one or more lower levels called subclasses or one or more higher levels called superclasses. A unique object or a specific occurrence of a class of objects is called an instance. Classes define: -attributes (state) -operations or methods (behavior).

Generalization hierarchy The relationship among the classes, subclasses, and superclasses form a hierarchy. A generalization hierarchy is an object- oriented design tool used to show the relationships among the classes of objects. TOOLBARS BUTTON BARS MENU BARS SCROLL BARS DROP-DOWN MENUS SHORTCUT MENUS LIST BOX MENUS

An operation, or service, is an activity that reads or manipulates the data of an object. Examples of operations include the standard mathematical, logical operations, input, output and storage operations associated with computer data. A method is the code used to perform the operation or service. For an object to do something, it must receive a message.

The message defines the interaction of the object. Everything an object can do is represented by the message. The message has 2 parts:  The name of the object to which the message is being sent.  The name of the method that will be performed.

The trigger that causes the message to be sent may come from another object or an external user. The entire process of a trigger sending a message that causes an operation to occur is called an event. For example, if you click a button to save data, that action is the trigger. A message is sent to the disk to prepare for input. Writing the data to the disk is the operation. Saving is the event.

In object-oriented terminology, the data stored about an object is referenced by an attribute, or property. Attributes are identifying characteristic of individual objects such as name, size, or color. Attributes should not be confused with the data itself. For example, color is an attribute; red is the data.

An Object Structure Diagram General Form of an Object Structure Diagram Object Structure Diagram Object name Object attributes Object methods Data Record length number of fields sequence number read write delete modify

Defining new classes To define a new Java class we must extend the class java.lang.Object eg. public class NameOfClass extends Object{…} Or public class NameOfClass {…..}

Final class Declares that the class cannot be subclassed. Two reasons : 1. to increase system security by preventing system subversion. 2. good object-oriented design. Eg. final class ChessAlgorithm {... }

Class attributes Attributes hold the state of an object. Eg. BankAccount object might include the attributes -accountNumber -accountOwner -overdraftLimit -balance

Example class defination public class BankAccount { private int accountNumber; private String accountOwner; private float overdraftLimit; private float balance; public void deposit(float amount) {….} public void withdraw( float amount) {….} public float getBalance() {…} public void increaseOverdraft (float amount) {….} public void decreaseOverdraft (float amount) {…} } Class name Attributes Methods

Using packages To use existing classes in another package we use the import statement. -import package; -import package.class; -import package.*; Example: -import java.awt; -import java.awt.Button; -import java.awt.*;

Abstract classes Abstract classes are classes that embody coherent and cohesive, but incomplete concepts. Provide starting points for inheritance. eg. Motor vehicle can be motor car. Motor bike, motor boat, lorry etc...

Creating abstract classes To make a class or method abstract we include the abstract keyword in the defination. Eg, public abstract class Shape {…}

Subclasses in Java To create a subclass of a Java class we use the extends keyword. We can only subclass one class at a time.( Java only supports single inheritance). public class Circle extends Shape{ ….. }

Control Access There are 3 types: 1.public 2.private 3.protected

Private control access A class’s private members are accessible only in the class methods. Public control access A class’s public members are accessible anywhere that the program has a reference to an object of the class or one of its subclasses. Protected control access A class’s protected members can be access by the class that define the members and its subclass.

Example:private class Wang { private int ringgit, sen; public Wang(int r, int s) { ringgit = r; sen= s; System.out.println(“Jumlah sen:” + jumlahSen()); } private int jumlahSen(){ return 100*ringgit + sen; } class Aplikasi { public static void main ( String [] args) { Wang wang = new Wang(5, 20); System.out.println(“sen: “ + wang.jumlahSen()); // Ralat }

Example: public class Wang { public int ringgit, sen; public Wang(int r, int s) { ringgit = r; sen= s; System.out.println(“Jumlah sen:” + jumlahSen()); } public int jumlahSen(){ return 100*ringgit + sen; } class Aplikasi { public static void main ( String [] args) { Wang wang = new Wang(5, 20); wang.ringgit = -100; System.out.println(“sen: “ + wang.jumlahSen()); }

Class Methods (operations) Methods are the operations that an object can perform or suffer. BankAccount object might have methods: -deposit() -withdraw() -getBalance() -increaseOverdraft() -decreaseOverdraft()

Method Body super If a method hides one of its superclass's member variables, you can refer to the hidden variable through the use of the super keyword. Similarly, if your method overrides one of its superclass's methods, your method can invoke the overridden method through the use of the super keyword.

Eg: class ASillyClass { boolean aVariable; void aMethod() { aVariable = true; } class ASillierClass extends ASillyClass { boolean aVariable; void aMethod() { aVariable = false; super.aMethod(); System.out.println(aVariable); System.out.println(super.aVariable); } Output: false true

Constructor. All Java classes have constructors that are used to initialize a new object of that type. A constructor has the same name as the class. Typically, a constructor uses its arguments to initialize the new object's state. When creating an object, choose the constructor whose arguments best reflect how you want to initialize the new object.

Encapsulation Encapsulation is the capability of an object to have data and functionality (methods) available to the user, without the user having to understand the implementation within the object. Encapsulation is the process of hiding the implementation and programming details of an object from its user, making those details transparent. Eg- stack (LIFO), the last item pushed (inserted) on the stack is the first item popped (removed) from the stack. The client of a stack class cares about what functionality a stack offers, but not about how that functionality is implemented.

Inheritance (Pewarisan) The process of defining new classes by reusing the features of existing classes. Only the differences between the new class and the existing class needs to be defined. Can be found by looking for ‘IS-A” relationships between objects. Eg. vehicle boatplane is a

Inheritance example car Sports car Estate car Superclass Sub-class More generalised More specialised

Polymorphism Polymorphism allows an instruction to be given to an object using a generalized, rather than a specifically detailed, command. The same command will get different, but predictable result depending on the object that receives the command. While the specific actions (internal to the object) are different, the result would be the same.

Polymorphism:Example SpaceObject draw() LaserBeamSpaceShipMartian

The Benefits of Object-Oriented Programming 1.Reusability The classes are designed so they can be reused in many systems, or modified classes can be created using inheritance. 2.Stability The classes are designed for repeated use and become stable over time. 3.Easier design The designer looks at each object as a black box and is not as concerned with the detailed inside. 4.Faster design The applications can be created from existing components.

API Specification