
OBJECT-ORIENTED PROGRAMMING MODULE #7 Best Practice Principles Objectives: Understand Class and Object Concepts Understand Polymorphic Concepts Understand Hierarchy and Inheritance Object-Oriented Programming !Object-oriented programming (OOP) is a methodology !C++, Java, 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 user interface • 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 programming language !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
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages29 Page
-
File Size-