Object-Oriented Programming Concepts. Contents 1.What is OOP? 2.Classes and Objects 3.Principles of OOP InheritanceInheritance AbstractionAbstraction.

Презентация:



Advertisements
Похожие презентации
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
Advertisements

2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Polymorphism.
1 When And How To Make Interfaces Work For You Advanced OO Techniques Alex Herbstritt Principal Software Engineer, Progress Software Session 102.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
© 2002 IBM Corporation Confidential | Date | Other Information, if necessary © Wind River Systems, released under EPL 1.0. All logos are TM of their respective.
WEB SERVICES Mr. P. VASANTH SENA. W EB SERVICES The world before Situation Problems Solutions Motiv. for Web Services Probs. with Curr. sols. Web Services.
1/27 Chapter 9: Template Functions And Template Classes.
1 © Luxoft Training 2012 Inner and anonymous classes.
Loader Design Options Linkage Editors Dynamic Linking Bootstrap Loaders.
Data Types in C. A Data Type A data type is –A set of values AND –A set of operations on those values A data type is used to –Identify the type of a variable.
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
In mathematics, the notion of permutation is used with several slightly different meanings, all related to the act of permuting (rearranging) objects.
© Luxoft Training 2013 Using Reflection API in Java.
Unit II Constructor Cont… Destructor Default constructor.
© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
What to expect? How to prepare? What to do? How to win and find a good job? BUSINESS ENGLISH COURSE NOVA KAKHOVKA GUMNASUIM 2012.
A class is just a collection of variables--often of different types--combined with a set of related functions. The variables in the class are referred.
HPC Pipelining Parallelism is achieved by starting to execute one instruction before the previous one is finished. The simplest kind overlaps the execution.
Транксрипт:

Object-Oriented Programming Concepts

Contents 1.What is OOP? 2.Classes and Objects 3.Principles of OOP InheritanceInheritance AbstractionAbstraction EncapsulationEncapsulation PolymorphismPolymorphism 2

What is OOP?

Object-oriented programming (OOP) is an engineering approach for building software systemsObject-oriented programming (OOP) is an engineering approach for building software systems Based on the concepts of classes and objects that are used for modeling the real world entitiesBased on the concepts of classes and objects that are used for modeling the real world entities Object-oriented programsObject-oriented programs Consist of a group of cooperating objectsConsist of a group of cooperating objects Objects exchange messages, for the purpose of achieving a common objectiveObjects exchange messages, for the purpose of achieving a common objective Implemented in object-oriented languagesImplemented in object-oriented languages 4

OOP in a Nutshell A program models a world of interacting objectsA program models a world of interacting objects Objects create other objects and send messages to each other (in Java, call each others methods)Objects create other objects and send messages to each other (in Java, call each others methods) Each object belongs to a classEach object belongs to a class A class defines properties of its objectsA class defines properties of its objects The data type of an object is its classThe data type of an object is its class Programmers write classes (and reuse existing classes)Programmers write classes (and reuse existing classes) 5

What are OOPs Claims To Fame? Better suited for team developmentBetter suited for team development Facilitates utilizing and creating reusable software componentsFacilitates utilizing and creating reusable software components Easier GUI programmingEasier GUI programming Easier software maintenanceEasier software maintenance All modern languages are object-oriented: Java, C#, PHP, Perl, C++,...All modern languages are object-oriented: Java, C#, PHP, Perl, C++,... 6

Classes and Objects

What Are Objects? Software objects model real-world objects or abstract conceptsSoftware objects model real-world objects or abstract concepts E.g. dog, bicycle, queueE.g. dog, bicycle, queue Real-world objects have states and behaviorsReal-world objects have states and behaviors Dogs' states: name, color, breed, hungryDogs' states: name, color, breed, hungry Dogs' behaviors: barking, fetching, sleepingDogs' behaviors: barking, fetching, sleeping 8

What Are Objects? How do software objects implement real- world objects?How do software objects implement real- world objects? Use variables/data to implement statesUse variables/data to implement states Use methods/functions to implement behaviorsUse methods/functions to implement behaviors An object is a software bundle of variables and related methodsAn object is a software bundle of variables and related methods 9

10 checks checks people people shopping list shopping list… numbers numbers characters characters queues queues arrays arrays Things in the real world computer Things in the computer world Objects Represent

Classes Classes provide the structure for objectsClasses provide the structure for objects Define their prototypeDefine their prototype Classes define:Classes define: Set of attributesSet of attributes Also called stateAlso called state Represented by variables and propertiesRepresented by variables and properties BehaviorBehavior Represented by methodsRepresented by methods A class defines the methods and types of data associated with an objectA class defines the methods and types of data associated with an object 11

Objects Creating an object from a class is called instantiationCreating an object from a class is called instantiation An object is a concrete instance of a particular classAn object is a concrete instance of a particular class Objects have stateObjects have state Set of values associated to their attributesSet of values associated to their attributes Example:Example: Class: AccountClass: Account Objects: Ivan's account, Peter's accountObjects: Ivan's account, Peter's account 12

Classes – Example 13 AccountAccount +Owner: Person +Ammount: double +Owner: Person +Ammount: double +suspend()+deposit(sum:double)+withdraw(sum:double)+suspend()+deposit(sum:double)+withdraw(sum:double) ClassClass AttributesAttributes OperationsOperations

Classes and Objects – Example 14 AccountAccount +Owner: Person +Ammount: double +Owner: Person +Ammount: double +suspend()+deposit(sum:double)+withdraw(sum:double)+suspend()+deposit(sum:double)+withdraw(sum:double) ClassClass ivanAccountivanAccount +Owner="Ivan Kolev" +Ammount= Ammount= peterAccountpeterAccount +Owner="Peter Kirov" +Ammount= Ammount= kirilAccountkirilAccount +Owner="Kiril Kirov" +Ammount=25.0 +Ammount=25.0 ObjectObject ObjectObject ObjectObject

Messages What is a message in OOP?What is a message in OOP? A request for an object to perform one of its operations (methods)A request for an object to perform one of its operations (methods) All communication between objects is done via messagesAll communication between objects is done via messages 15

Interfaces Messages define the interface to the objectMessages define the interface to the object Everything an object can do is represented by its message interfaceEverything an object can do is represented by its message interface The interfaces provide abstractionsThe interfaces provide abstractions You shouldn't have to know anything about what is in the implementation in order to use it (black box)You shouldn't have to know anything about what is in the implementation in order to use it (black box) An interface is a set of operations (methods) that given object can performAn interface is a set of operations (methods) that given object can perform 16

The Principles of OOP

InheritanceInheritance AbstractionAbstraction EncapsulationEncapsulation PolymorphismPolymorphism 18

Inheritance A class can extend another class, inheriting all its data members and methodsA class can extend another class, inheriting all its data members and methods The child class can redefine some of the parent class's members and methods and/or add its ownThe child class can redefine some of the parent class's members and methods and/or add its own A class can implement an interface, implementing all the specified methodsA class can implement an interface, implementing all the specified methods Inheritance implements the is a relationship between objectsInheritance implements the is a relationship between objects 19

Inheritance TerminologyTerminology 20 subclass or derived class superclass or base class extends subinterfacesuperinterface extends classinterface implements

Inheritance 21 PersonPerson +Name: String +Address: String +Name: String +Address: String EmployeeEmployee +Company: String +Salary: double +Company: String +Salary: double StudentStudent +School: String SuperclassSuperclass SubclassSubclassSubclassSubclass

Inheritance in Java In Java, a subclass can extend only one superclassIn Java, a subclass can extend only one superclass In Java, a subinterface can extend one superinterfaceIn Java, a subinterface can extend one superinterface In Java, a class can implement several interfacesIn Java, a class can implement several interfaces This is Javas form of multiple inheritanceThis is Javas form of multiple inheritance 22

Interfaces and Abstract Classes in Java An abstract class can have code for some of its methodsAn abstract class can have code for some of its methods Other methods are declared abstract and left with no codeOther methods are declared abstract and left with no code An interface only lists methods but does not have any codeAn interface only lists methods but does not have any code A concrete class may extend an abstract class and/or implement one or several interfaces, supplying the code for all the methodsA concrete class may extend an abstract class and/or implement one or several interfaces, supplying the code for all the methods 23

Inheritance Benefits Inheritance plays a dual role:Inheritance plays a dual role: A subclass reuses the code from the superclassA subclass reuses the code from the superclass A subclass inherits the data type of the superclass (or interface) as its own secondary typeA subclass inherits the data type of the superclass (or interface) as its own secondary type 24

Class Hierarchies Inheritance leads to a hierarchy of classes and/or interfaces in an application:Inheritance leads to a hierarchy of classes and/or interfaces in an application: 25 Game GameFor2 BoardGame ChessBackgammon Solitaire

Inheritance An object of a class at the bottom of a hierarchy inherits all the methods of all the classes aboveAn object of a class at the bottom of a hierarchy inherits all the methods of all the classes above It also inherits the data types of all the classes and interfaces aboveIt also inherits the data types of all the classes and interfaces above Inheritance is also used to extend hierarchies of library classesInheritance is also used to extend hierarchies of library classes Allows reusing the library code and inheriting library data typesAllows reusing the library code and inheriting library data types 26

Abstraction Abstraction means ignoring irrelevant features, properties, or functions and emphasizing the relevant ones...Abstraction means ignoring irrelevant features, properties, or functions and emphasizing the relevant ones relevant to the given project (with an eye to future reuse in similar projects)... relevant to the given project (with an eye to future reuse in similar projects) Abstraction = managing complexityAbstraction = managing complexity 27 Relevant to what?

Abstraction Abstraction is something we do every dayAbstraction is something we do every day Looking at an object, we see those things about it that have meaning to usLooking at an object, we see those things about it that have meaning to us We abstract the properties of the object, and keep only what we needWe abstract the properties of the object, and keep only what we need Allows us to represent a complex reality in terms of a simplified modelAllows us to represent a complex reality in terms of a simplified model Abstraction highlights the properties of an entity that we are most interested in and hides the othersAbstraction highlights the properties of an entity that we are most interested in and hides the others 28

Abstraction in Java In Java abstraction is achieved by use ofIn Java abstraction is achieved by use of Abstract classesAbstract classes InterfacesInterfaces 29

Abstract Data Types Abstract Data Types (ADT) are data types defined by a set of operationsAbstract Data Types (ADT) are data types defined by a set of operations Examples:Examples: 30

Abstraction in AWT/Swing java.lang.Objectjava.lang.Object | | +--java.awt.Component +--java.awt.Component | | +--java.awt.Container +--java.awt.Container | | +--javax.swing.JComponent +--javax.swing.JComponent | | +--javax.swing.AbstractButton +--javax.swing.AbstractButton 31

Encapsulation Encapsulation means that all data members (fields) of a class are declared privateEncapsulation means that all data members (fields) of a class are declared private Some methods may be private, tooSome methods may be private, too The class interacts with other classes (called the clients of this class) only through the classs constructors and public methodsThe class interacts with other classes (called the clients of this class) only through the classs constructors and public methods Constructors and public methods of a class serve as the interface to classs clientsConstructors and public methods of a class serve as the interface to classs clients 32

Encapsulation Ensures that structural changes remain local:Ensures that structural changes remain local: Usually, the internal structure of a class changes more often than the classs constructors and methodsUsually, the internal structure of a class changes more often than the classs constructors and methods Encapsulation ensures that when fields change, no changes are needed in other classes (a principle known as locality)Encapsulation ensures that when fields change, no changes are needed in other classes (a principle known as locality) Hiding implementation details reduces complexity easier maintenanceHiding implementation details reduces complexity easier maintenance 33

Encapsulation – Example Data Fields are privateData Fields are private Constructors and accessor methods are definedConstructors and accessor methods are defined 34

Polymorphism Ability to take more than one formAbility to take more than one form A class can be used through its parent class's interfaceA class can be used through its parent class's interface A subclass may override the implementation of an operation it inherits from a superclass (late binding)A subclass may override the implementation of an operation it inherits from a superclass (late binding) Polymorphism allows abstract operations to be defined and usedPolymorphism allows abstract operations to be defined and used Abstract operations are defined in the base class's interface and implemented in the subclassesAbstract operations are defined in the base class's interface and implemented in the subclasses 35

Polymorphism Why use an object as a more generic type?Why use an object as a more generic type? To perform abstract operationsTo perform abstract operations To mix different related types in the same collectionTo mix different related types in the same collection To pass it to a method that expects a parameter of a more generic typeTo pass it to a method that expects a parameter of a more generic type To declare a more generic field (especially in an abstract class) which will be initialized and specialized laterTo declare a more generic field (especially in an abstract class) which will be initialized and specialized later 36

Polymorphism – Example 37 Square::calcSurface() { return size * size; return size * size;} Circle::calcSurface() { return PI * radius * raduis; return PI * radius * raduis;} Abstract class Abstract action Concrete class Overriden action

Polymorphism Polymorphism ensures that the appropriate method is called for an object of a specific type when the object is disguised as a more generic type:Polymorphism ensures that the appropriate method is called for an object of a specific type when the object is disguised as a more generic type: 38 Figure f1 = new Square(...); Figure f2 = new Circle(...); // This will call Square::calcSurface() int surface = f1.calcSurface(); // This will call Square::calcSurface() int surface = f2.calcSurface();

Polymorphism in Java Good news: polymorphism is already supported in JavaGood news: polymorphism is already supported in Java All you have to do is use it properlyAll you have to do is use it properly Polymorphism is implemented using a technique called late method binding:Polymorphism is implemented using a technique called late method binding: Exact method to call is determined at run time before performing the callExact method to call is determined at run time before performing the call 39

Questions? OOP Concepts

Problems 1.Describe the term object in OOP. 2.Describe the term class in OOP. 3.Describe the term interface in OOP. 4.Describe the term inheritance in OOP. 5.Describe the term abstraction in OOP. 6.Describe the term encapsulation in OOP. 7.Describe the term polymorphism in OOP. 41