Object Oriented Programming Basic Concepts What is objectorientation

Object Oriented Programming Basic Concepts

What is object-orientation all about? • Principles and techniques for system modeling which: – Aim to produce a model of a system – manage complexity inherent in analysis, design, and implementation – provide methodology for system development – provide integrated view of hardware and software

How? “Using object-orientation as a base, we model a system as a number of objects that interacts. ”

Object Oriented System (OOS) • Modelling the system by representing a collection of objects in it • Each object represents some part of the system that is independent of other parts • The different objects can collaborate to perform a task • Interaction between objects through messages

Is it any good? A system which is designed and modeled using an object-oriented technology is: • Easy to understand • Directly related to reality - reduces the semantic gap between reality and models • Natural partitioning of the problem • More flexible and resilient to change - allows local modification to models • Systems can be developed more rapidly and at a lower cost

What is an object? • • • Tangible Things as a car, printer, . . . Roles as employee, boss, . . . Incidents as flight, overflow, . . . Interactions as contract, sale, . . . Specifications as colour, shape, … 6

what is an object? • an object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain. • An object is anything to which a concept applies. • An object is a “noun”

How to define an object? • An object is defined by: – Attributes (also called fields) – Behaviour---what it can do? (also called methods) • Example A man can be defined by: – Attributes: name, age, job, address, . . etc – Behaviour: talk, walk, eat, work, study, . . . etc • How to define a bird? A car? A flight?

Class • A class represents a template for several objects and describes how these objects are structured internally • Objects of the same class have the same definition both for their operations and their information structure • Classes are types of things

Instance • An instance is an object created from a class • A class describes the behaviour and information structure of an instance, while the current state of the instance is defined by the operations performed on the instance • System’s behaviour is performed via the interactions between instances

Class vs. Object • Class People – John , George, Sara are objects that can be instantiated from the people class • Class Vehicle – Bus, car, train are objects (instances of the vehicle class) • Class Car – The blue Nissan, the red Vauxhall, my uncle’s car are objects (instances of the car class)

Class vs. Object (cont. ) • Find a class to represent the following items: – dog, cat, lion, tiger – chair, table, wardrobe – banana, orange, apple – breakfast, lunch, dinner • Provide examples of objects that can be instantiated from the following classes – Students, Courses, Modules

Object-oriented Programming • What is OOP? – OOP is a programming paradigm based on three simple core concepts: • Classes: – Are “types” of “things” that we can talk about. • Objects: – Are “instances” or examples of classes. • Message passing: – Objects interact with each other by passing messages to ask each other to carry out their methods (behaviours).

Why OO programming? • Modularity - large software projects can be split up in smaller pieces. • Reuseability - Programs can be assembled from pre-written software components. • Extensibility - New software components can be written or developed from existing ones.

Object-oriented Basics • Fundamental concepts – Object, Class, Field/attribute, Method, Parameter • Objects – Represent “things” from the real world, or from some problem domain (e. g. “the red car in the car park”) • Classes – Objects are created from classes – Represent all objects of a kind (e. g. all “cars”)

Object-oriented Basics • Fields/attributes – Classes define fields (e. g. a Person class could contain fields name, age, sex, etc. ) – Objects have attributes, which are values stored in fields (e. g. person_1 has attributes “John Smith”, 25, Male) – An object’s state is defined by its attributes • Methods – Classes define methods, which can access or change attributes – Objects communicate by calling (invoking) each others’ methods • Parameters – Methods can have parameters to pass additional information during execution

What is a Class Diagram? • A class diagram is a graphical representation that describes the types of objects in the system and the various kinds of relationships that exist among them. • A central modeling technique that runs through nearly all object-oriented methods. • The richest notation in UML.

Essential Elements of a UML Class Diagram • Class name (top) • Attributes/ fields (middle) • Methods (bottom) Class Name Attributes Methods • Links between different classes define relationships (will be covered later) Student name: string age: int Attend. Lecture() Study()

OO Principle: Encapsulation An object is like a black box. The internal details are hidden. Encapsulation is the practice of including in an object everything it needs hidden from other objects. The internal state is usually not accessible by other objects.

Encapsulation (cont. ) • A concept of ‘Self-containing’ • Information hiding - ‘internal’ structure is hidden from their surroundings • Behaviours (methods) and information (attributes) are represented or implemented internally • Functionality and behaviour characterised by ‘interfacing’ operations • This is achieved by the use of classes and the access modifiers

Access Modifiers • Access modifiers aid to achieve the encapsulation principle by preventing unauthorized or accidental access to attributes and methods. Through which you can control how the methods and attributes can be accessed: public class Person { private string name; private int age; public void Work() { } }

Access Modifiers (cont. ) Accessibilities options – public – Accessible to all – private – Accessible to containing class – protected – Accessible to containing or derived classes • In most cases: fields are private or protected, and methods are public.

Interaction between Objects • Objects communicate through messages (invoking or calling methods of other objects) – Methods: mostly public – Fields: Private or protected • You send messages to an object by making method calls. • However, we need to create objects (instances) from classes first

Class Instantiation • A class is a specification or design and an instance is a “real world” occurrence of the design • Just as someone has to build a car from its engineering drawings before you can actually drive it, you must build an object of a class before you can perform the tasks the class describes. • You can have as many instances as you like from a single class

Declaring & Instantiating Objects • Creating objects is similar to declaring variables and can be done using the syntax: <Class Name> <Object Name>; • Example: int x 1; Person P 2; OR alternatively, you can declare and initialize an object using the constructor method <Object Name> = new <Class Name>; • Example: Person P 1 = new Person(“John”); Person P 2 = new Person(“Adam”); int x 1 = 5;

Some Important Methods • Constructor method: to initiate an object by sending its values as parameters • Accessor method: to get information about an object • Mutator methods to mutate (change) an object’s state • Printing method: to specify how to print an object

Constructors • Constructors create and initialize an object to some initial state • They must have the same name as their class • They store, or assign, initial values into the fields • They often receive external parameter values for this (e. g. person. Name) • Some classes may have more than one constructor to initialize objects in different ways • But all constructors will still have the same name as the class

Constructor Example Public class Person { string name; int age; // Constructor public Person( string n , int a) { name = n; age = a; } } public static void Main(string[] args) { Person P 1 = new Person(”John”, 15); Person P 2 = new Person (“Adam”, 27); }

Accessor Methods • Methods implement the behavior of objects • Methods have a structure consisting of a header and a body • The header defines the method’s signature • public int Get. Age() • The body (within curly brackets) encloses the method’s statements – Which implement the behaviour • Accessor methods provide information about an object – In general they return a value of a field through a return statement

Accessor Methods return type access modifier public int Get. Age() { return age; } method name parameter list (empty in this case) return statement start and end of method body (block) • Because accessor methods have a return type they must have at least one return statement

Summary obj 1: Person name john private public encapsulation class Person Name : string age 25 sex M get. Name “john” Age: int Sex: char Get. Name() Get. Age() Get. Sex() obj 2: Person name jane age 22 sex F 22 get. Age – Methods get. Name, get. Age can be called by other objects (not shown) to get or access attributes (called accessor methods) – Other methods can be defined, which change an object’s attributes (i. e. state) (called mutator methods)
- Slides: 31