Object-Oriented Programming Concepts

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Classes & Objects Computer Science I Last updated 9/30/10.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
1 CS1001 Lecture Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object-oriented Programming Concepts
1 CS1001 Lecture Overview Object Oriented Design Object Oriented Design.
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
2.5 OOP Principles Part 1 academy.zariba.com 1. Lecture Content 1.Fundamental Principles of OOP 2.Inheritance 3.Abstraction 4.Encapsulation 2.
2.5 OOP Principles Part 2 academy.zariba.com 1. Lecture Content 1.Polymorphism 2.Cohesion 3.Coupling 2.
Abstraction, Inheritance, and Polymorphism in Java.
Svetlin Nakov Telerik Corporation
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
An Object-Oriented Approach to Programming Logic and Design
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Object Oriented Programming
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Object-Oriented Programming: Inheritance and Polymorphism.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Introduction to Object-oriented Programming
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object-Oriented Design
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING
JAVA By Waqas.
The Movement To Objects
Inheritance ITI1121 Nour El Kadri.
University of Central Florida COP 3330 Object Oriented Programming
Final and Abstract Classes
Inheritance and Polymorphism
Chapter 11 Object-Oriented Design
OOP What is problem? Solution? OOP
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Analysis and Design
Lecture 23 Polymorphism Richard Gesick.
Inheritance Basics Programming with Inheritance
Corresponds with Chapter 7
Lecture 22 Inheritance Richard Gesick.
Advanced Programming Behnam Hatami Fall 2017.
Computer Programming with JAVA
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Object-Oriented Programming: Inheritance and Polymorphism
Fundaments of Game Design
Workshop for Programming And Systems Management Teachers
Object-Oriented Programming
CIS 199 Final Review.
Object-Oriented PHP (1)
CPS120: Introduction to Computer Science
Final and Abstract Classes
Object-Oriented Programming
Lecture 10 Concepts of Programming Languages
C++ Object Oriented 1.
Computer Science II for Majors
Presentation transcript:

Object-Oriented Programming Concepts 1

Contents What is OOP? Classes and Objects Principles of OOP Inheritance Abstraction Encapsulation Polymorphism Cohesion and Coupling

What is OOP? Object-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 entities Object-oriented programs Consist of a group of cooperating objects Objects exchange messages, for the purpose of achieving a common objective Implemented in object-oriented languages

OOP in a Nutshell A program models a world of interacting objects Objects create other objects and “send messages” to each other (in C#, call each other’s methods) Each object belongs to a class A class defines properties to its object The data type of an object is its class Programmers write classes (and reuse existing classes)

Classes and Objects

What are Objects? Software objects model real-world objects or abstract concepts E.g. dog, bicycle, queue Real-world objects have states and behaviors Dogs’ states: name, color, breed Dogs’ behaviors: barking, fetching, sleeping

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

Classes Classes provide the structure for objects Classes define: Define their prototype Classes define: Set of Attributes Also called State Represented by variables and properties Behavior Represented by methods A class defines the methods and types of data associated with an object

Objects Creating an object from a class is called instantiation An object is a concrete instance of a particular class Objects have state Set of values associated to their attributes Example Class: Sprite Objects: Mario sprite, Mushroom sprite

Classes - Example Class Attributes Sprite Operations private Texture2D: texture private Vector2: position public Update (GameTime gameTime, GraphicsDeviceManager graphics) public Draw (SpriteBatch spriteBatch) Operations

Classes and Objects - Example MarioSprite private Texture2D: texture = …<Texture2D>(“mario") private Vector2: position = (200, 100) Sprite private Texture2D: texture private Vector2: position Object public Update (GameTime gameTime, GraphicsDeviceManager graphics) public Draw (SpriteBatch spriteBatch) MushroomSprite private Texture2D: texture = …<Texture2D>(“mushroom") private Vector2: position = (300, 300)

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

Interfaces Messages define the interface to the object Everything an object can do is represented by its message interface The interfaces provide abstractions 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 perform

The Principles of OOP

The Principles of OOP Inheritance Abstraction Encapsulation Inherit members from a parent (base) class Abstraction Define and execute abstract actions Encapsulation Hide the internals of a class Polymorphism Access a class through its parent (base) interface

Inheritance Inheritance allows child classes to inherit the characteristics of existing parent class Attributes (fields and properties) Operations (methods) A child class can extend the parent class Add new fields and methods Redefine methods (modify existing behaviors) A class can implement an interface, providing implementation for all the specified methods Inheritance implements the “is a” relationship between objects

Inheritance Terminology Derived class inherits Base class Class implements interface

UserControlledSprite Inheritance Base class Sprite private Texture2D: texture private Vector2: position abstract Vector2 direction () Derived Derived UserControlledSprite AutomatedSprite private Texture2D: texture = …<Texture2D>(“mario") private Vector2: position = (200, 100) public override Vector2 direction () { … } private Texture2D: texture = …<Texture2D>(“mushroom") private Vector2: position = (300, 300) public override Vector2 direction () { … }

Inheritance In C#, a derived class can extend only one base class In C#, a class can implement multiple interfaces This is C#’s form of multiple inheritance

Interfaces and Abstract Classes An abstract class can have code for some of its methods Other methods are declared abstract and left with no code An 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 methods

Inheritance Benefits Inheritance plays a dual role: A derived class reuses the code from the base class A derived class inherits the data type of the base class (or interface) as its own secondary type

Class Hierarchies Inheritance leads to a hierarchy of classes and/or interfaces in an application: Game Solitaire Multi-player Game Board Game Backgammon Monopoly

Inheritance An 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 above Inheritance is also used to extend hierarchies of library classes Allows reusing the library code and inheriting library data types

How to define Inheritance We specify the name of the base class after the name of the derived public class Shape { … } public class Circle : Shape In the constructor of the derived class we use the keyword base to invoke the constructor of the base class public Circle () : base () { … }

Accessibility Levels Access modifiers in C# public – access is not restricted private – access is restricted to the containing type protected – access is limited to the containing type and types derived from it internal – access is limited to the current assembly protected internal – access is limited to the current assembly or types derived from the containing class

Abstraction 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) Abstraction = managing complexity “Relevant to what?

Abstraction Abstraction is something we do every day Looking 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 need Allows 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 others

Abstraction in C# Abstraction is achieved by use of Abstract classes Interfaces <<interface>> IController bool IsExitState() UpdateInput () <<abstract>> Controller protected bool[] inputState KeyboardController GamePadController

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

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

Encapsulation - Example Data fields are private Constructors and accessor methods are defined Sprite private Texture2D: texture private Vector2: position Sprite (Texture2D texture) public Vector2D Position { return position; } public Position (Vector2D position) { this.position = position; }

Polymorphism Ability to take more than one form A class can be used through its parent class’s interface A derived class may override the implementation of an operation it inherits from a base class (late binding) Polymorphism allows abstract operations to be defined and used Abstract operations are defined in the bass class’s interface and implemented in the derived class Declared as abstract or virtual

Polymorphism Why use an object as a more generic type? To invoke abstract operations To mix different related types in the same collection E.g. List <Object> can hold anything To 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” later

Virtual Methods A virtual method is a method that can be used in the same way on instances of base and derived classes but its implementation is different A method is said to be a virtual when it is declared as virtual Methods that are declared as virtual in a base class can be overridden using the keyword override in the derived class public virtual double CalcArea ()

The override Modifier Using override we can modify a method or property An override method provides a new implementation of a member inherited from a base class You cannot override a non-virtual or static method The overridden base method must be virtual, abstract, or override

Polymorphism - Example Shape public virtual double CalcArea () Rectangle Circle private Rectange2D rectangle private Point center private int radius public override double CalcArea () { return rectangle.Width () * rectangle.Height (); } public override double CalcArea () { return PI * radius * radius; }

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: Shape shape1 = new Rectangle (); Shape shape2 = new Circle (); // this will invoke Rectangle:CalcArea () double area1 = shape1.CalcArea (); // this will invoke Circle:CalcArea () double area2 = shape2.CalcArea ();

Cohesion Cohesion describes how closely all the routines in a class or all the code in a routine support a central purpose Cohesion must be strong Well defined abstractions keep cohesion strong Classes must contain strongly related functionality and aim for single purpose Cohesion is a useful tool for managing complexity

Coupling Coupling describes how tightly a class or routine is related to other classes or routines Coupling must be kept loose Modules must depend little on each other All classes and routines must have small, direct, visible, and flexible relations to other classes and routines One module must be easily used by other modules

Summary OOP fundamental principals are: inheritance, encapsulation, abstraction, polymorphism Inheritance allows inheriting members from another class Abstraction and encapsulation hide internal data and allow working through abstract interface Polymorphism allows working with objects through their parent interface and invoke abstract actions Strong cohesion and loose coupling avoid spaghetti code

?

References Telerik Software Academy