Quick viewing(Text Mode)

Object-Oriented Programming

Object-Oriented Programming

OBJECT-ORIENTED PROGRAMMING

MODULE #7 Best Practice Principles

Objectives: Class and Object Concepts Understand Polymorphic Concepts Understand Hierarchy and Inheritance Object-Oriented Programming

!Object-oriented programming (OOP) is a methodology !++, , C# are OOP languages !Object-oriented principles •Focus on data objects verses functional components (functions) •Data and behavior are linked together •Objects are responsible for their behavior •Objects control access to their data •Objects are an autonomous part of a program !OOP messages (function/method calls) • passed to object methods (function/method definitions) to perform an action (behavior) !Traditional programs use functions to accept, change and return data Object-Oriented Overview

★ Object-Oriented Concepts •Features: data abstraction, encapsulation •Components: class, objects, attributes, methods, messages ★ Object-Oriented Analysis and Design •Features: inheritance, polymorphism, IS_A and HAS_A relationships •Components: class definition, object instantiation, message calling ★ Language Syntax •Keywords •Statements •Expressions ★ Program Development and Execution •Class Constructions •Core Class Library (e.g. .Net, Java and C++ base class libraries) •Development Environment (e.g. Visual Studio), Xcode, Netbeans) •Runtime Environment (e.g. .NET, JRE, C++ executable module) Object-Oriented Design

!Emphasis on data representation •Describe real-world entity •Declare data types !Define the public interface •Specify how data is used •Control data access !Implement methods •Describe object behavior •Accept messages •Manipulate data !Purpose of OO development •Reduce cost by condensing coding time •Reusable code to improve stability •Hide complexity •Simplify maintenance Object Oriented Core Definition

!Data abstraction is the ability to define new data types from existing data types !Class is a user-defined data type composed of data and function members !Object is a class variable, a class instance !Instantiation means to create an instance or to declare an object of a particular data type, a variable declaration !Message is a request for action from the public interface of an object, a function call !Public interface is the program access for sending messages to object methods !Method is a class member function definition !Member function is a function definition associated with a class definition !Data member is a variable declaration associated with a class definition !Data hiding a technique that insulates data from direct access Object-Oriented Elements

!Encapsulation – protection • The ability to hide the internal representation of data and implementation details of functions • Class definition !Data Abstraction - creation • The representation of information in terms of its • Class public interface !Inheritance - generation • The uses of an existing class as a base for creating a new class that inherits its properties • Derived class !Polymorphism - variation • The uses of a name (e.g. function, class) or symbol (e.g. operator) to represent many different actions • Overloaded functions and operators What is a Class?

★Class [ e.g. class className { } ] •Software template that defines real world objects •Composed of methods (behaviors) & attributes (variables) •Template which defines methods and attributes to include in an object ★Instance(s) are objects that belong to a particular type of class •The process of creating an object from a class is called instantiation. •An object is an instance of a particular class. ★Methods and attributes are defined once in the class definition ★Each object instance contains data values to represent its own characteristics Class Keyword

!Vehicle for translating a data abstraction to a user-defined type !Class declaration •Specify the characteristics •Specify the operations •Contain data members and/or function members (methods) !Class member access •Membership operator (period) . !Keywords for class declaration •class keyword !Class Scope •Data and function member names defined within a class Class Components !A combination of data representation and data manipulation (encapsulation) !Class definition describes the components !Class method definition describes the implementation, supplies the details, behavior !Data members • Fundamental • Derived • User-defined !Function members • Messages • Public interface • Methods !Class access control (visibility) • Public • Protected • Private Class Program

"using directive - import predefined namespaces "namespace keyword - define program specific container for classes/code "public keywords - access specifiers "int, float - simple data types "string - reference type "return keyword - return a message/value from a behavior/method "Console.WriteLine() - class and method used to display a text value

C# Example What is an Object?

★Object [e.g. new className() ] •An instantiated class •Composed of behaviors & attributes ★Software object concept •Arose out of the need to model real-world objects in computer simulations ★An object is a software package that contains •A collection of related procedures •A collection of related attributes ★Methods are the object-oriented name for procedures or functions ★Attributes are data elements or variables Object Vehicle Explained

★Vehicle object (everything an object can do is expressed in its methods, its behavior) •Moving to a location •Loading contents •Unloading contents ★Vehicle information (everything an object knows is expressed in its variables, its attributes) •Contents •Size •Speed •Capacity ★Objects are self-contained and independently maintained What is a Message?

★Message [e.g. objRef.methodName()] •Communication between objects ★Messages are used by objects to interact ★A message is the name of an object followed by the name of the method ★Parameters are a collection of data elements that provide additional information ★A Sender is the object that initiates a message and a Receiver is the object that receives the message Object Messaging ★The message is the vehicle that activate objects to interact with each other ★A message is composed of three parts •Name of the receiving object •Name of the executing method •Optional parameters for a method ★Message syntax of object-oriented languages • C# obj.method(optional-parms) • Java obj.method(optional-parms) • C++ obj.method(optional-parms) ★Message may be one or two way communication •Sender send a message to the receiver •Receiver processes the message and sends a response •Return Value - the response from an object •The return value can be an object or a simple data type Object-Oriented Messaging Program

★ using directive - import predefined namespaces ★ namespace keyword - define program specific container for classes/code ★ private/public keywords - access specifiers ★ constructors - methods with the same name as the class; called when an object is instantiated ★ this keyword - identifies the object instance attribute (vs the local variable with the same name) ★ int, float, string - data types ★ return keyword - return a message/value from a behavior/method

C# Example What is Encapsulation?

!Encapsulation is “the placing of data and behavior together” !Encapsulation promotes information hiding •Hides the data (shields the data from direct access) •Hides the implementation details of the behavior !Data is protected from outside objects •Protects the variables from being corrupted •Keeps other objects from the complication of depending on its internal structure !Data is accessed only through its methods •Accessors - “getters” used to get/retrieve an attribute •Mutators - “setters” used to set an attribute of an object •Objects send messages to other objects •Messages call methods into action •Methods of the called object will access its data !An object is only required to know how to ask for the other objects information •The effects of change are minimized (e.g. internal structure) Encapsulation Programs

C++

C# Java What is Data Abstraction?

!Data Abstraction is the process of defining new kinds of data structures to describe real-world objects •It raises software design to a higher level •Older languages limit data to built-in types !An abstract data type is a definition of a new data type created by combining existing date types in a new way •Extract the essential characteristics •Its simplest form is a new class assembled from built-in data types !The object-oriented approach allows you to think at the level of the real world system instead of the level of a !Object technology is based on abstract data types •Object-oriented languages ( C#, Java, C++) are specifically designed to be extended and adapted to specialized needs •Class keyword is the tool used to create new data types •New data types are treated as though they are built into the language (not second-class citizens) Composite Data Abstraction

!A Composite data type is an abstract data type with other objects as attributes •Objects may contain other objects •It represents a more complex structure than simple objects (which contain only built-in data types) !Objects contained in composite objects may, themselves, be composite objects •Object nesting is infinite •It defines a HAS_A relationship •A complex, deeply-nested structure is treated as a single, unified object Abstraction - Data and Composition

C++ Java C# What is Inheritance?

!A mechanism in which one class is defined as a special case (subclass) of a more general class (superclass) !Defines an IS_A relationship •Automatically includes the method and variable definitions of the general class !A feature that has become central to object-oriented technology •Allows classes to be defined in terms of each other !Superclass •The “more general” class; the base or parent class !Subclass •Special case of a superclass; derived or child class •Can define its own unique methods and variables •Can override inherited characteristics Inheritance Hierarchy

!How an object finds a method when it receives a message •Looks in the immediate class of the class hierarchy •If not found, looks in its superclasses until the definition is found •Once found, method is executed !The class hierarchy is search •Methods defined at one level of the class hierarchy are available to the object for use Class Hierarchy

★ Class Hierarchy • The tree-like structure that describes the class relationship between • superclasses and subclasses • base classes and derived classes • parent classes and child classes ★ Nested classes • Inheritance will automatically accumulate through all levels of the hierarchy ★ The invention of the class hierarchy is the true genius of object-oriented technology • Human knowledge is structured in this manner • Generic concepts are refined into specialized cases ★ Object-oriented technology uses the same conceptual mechanisms humans employ in everyday life for building complex software solutions and systems Object-Oriented Inheritance Programs

C++ C# Java What is Polymorphism?

!Greek term for “many forms” !The ability to hide alternative behaviors (procedures/methods) behind a common interface !The object-oriented feature implemented by reusing the same method name !Overload a method •A method name is reused multiple times in one class •Each method definition must have a different signature (parameters must differ in type and/or number of parameters) •“A method in the same class with a different signature” !Override a method •A method name is reused multiple times in a class hierarchy •The method definitions must have the same signature (method parameters) •“A method in a subclass with the same signature” !Objects respond differently to the same message Polymorphism - Reusing Names

★ The ability of one type to appear as, and be used like, another type ★ The ability of objects belonging to different types to respond to method calls of the same name (each one according to a type-specific behavior) ★ Polymorphism purpose - implement message-passing in which objects of various types define a common interface of operations ★ Method naming is important • Keep the number of names to a minimum • Simplifies the programming process • Reduces the information load ★ Overloading - same method name reused/defined within a class • Compile-time polymorphism (early binding) • Compiler knows/selects the methods during compilation ★ Overriding - same method name reused/defined within a class hierarchy • Run-time polymorphism (late binding) - inheritance based • Compiler selects the method during execution

Draw(x,y) Polymorphism Program

!Vehicle implements the overload with the two setSize methods !Train implements one override methods with setSize !Note: the code also shows an overload of the getSize method

C# Java C++ Class Constructors !Class member function(s) with the same name as its class !Declare/construct values of its class type !Called when an object of its class is instantiated !Used to initialize class object data !Default constructor • Contain no arguments • Contain default arguments • Will be generated by the compiler if no constructor has been declared !Can be overloaded !Cannot specify a return type

C++ C# Java Class Destructor - C++ and C# Only

!Class member function with the same name as its class, preceded with a tilde ~ (C++ and C#. Descriptors do not exist for Java) !Destroy values of the class object !Called when an object of its class is going out of scope (C++) !Are non-deterministic for C# (guaranteed to be called before shutdown) !Cannot specify arguments or a return type