<<

Object oriented languages

dr inż. Marcin Pietroń Object oriented programming

• Object languages: - Java (business applications, mobile software etc.) - ++ (embedded, real-time, finance- sector) - Python (prototyping, testing) - - Ruby (web) - Objective-C (mobile) - … Object oriented programming

• programming paradigm - data structures called "objects", contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. Object oriented programming

• a feature of objects is that an object's procedures can access and often modify the data fields of the object with which they are associated, • in OO programming, objects in a software system interact with one another. Object oriented programming

• There is significant diversity in object-oriented programming, but most popular languages are class-based, meaning that objects are instances of classes, which typically also determines their type. Object oriented programming

Languages that support object-oriented programming use inheritance for code reuse and extensibility in the form of either classes or prototypes. Those that use classes support two main concepts: • Objects - structures that contain both data and procedures • Classes - definitions for the data format and available procedures for a given type or class of object; may also contain data and procedures (class methods) Object oriented programming

• Objects sometimes correspond to things found in the real world (e.g. a graphics program may have objects such as „figure ", "circle ", "square" or " cursor ", online shop may have objects such as "shopping cart" "customer" " basket " or "product ", • Objects can represent more abstract things and entities, like an object that represents an open file, or an object which provides some service (e.g. xml parsing)

Object oriented programming

Object is an instance of a particular class. Procedures in object- oriented programming are known as methods, variables are also known as fields, members, attributes or properties. • Class variables - belong to the class as a whole; there is only one copy of each one • Instance variables or attributes - data that belongs to individual objects; every object has its own copy of each one • Member variables - refers to both the class and instance variables that are defined by a particular class • Class methods - belong to the class as a whole and have access only to class variables and inputs from the procedure call • Instance methods - belong to individual objects, and have access to instance variables for the specific object they are called on, inputs, and class variables

Class definition class TComplex { private: double fRe; // the real part double fIm; // the imaginary part

public: // Getters / Setters double GetRe( void ) const { return fRe; } double GetIm( void ) const { return fIm; }

void SetRe( double re ) { fRe = re; } void SetIm( double im ) { fIm = im; } };

Class definition public class Complex {

private double fRe; private double fIm;

public void setRe(double re) {fRe = re;} public void setIm(double im) {fIm = im;}

public double getRe() {return fRe;} public double getIm() {return fIm;} } Definicja klasy public class Complex {

private double fRe; private double fIm;

public void setRe(double re) {fRe = re;} public void setIm(double im) {fIm = im;}

public double getRe() {return fRe;} public double getIm() {return fIm;}

public static void length(Complex c) { … } } Object oriented programming

• „Pure" OO languages, because everything in them is treated consistently as an object, from primitives such as characters and punctuation, all the way up to whole classes, prototypes, blocks, modules, etc. Examples: Eiffel, Obix, Ruby, Scala, Smalltalk, Self. • Languages designed mainly for OO programming, but with some procedural elements. Examples: Delphi/Object Pascal, C++, Java, C#, VB.NET. • Languages that are historically procedural languages, but have been extended with some OO features. Examples: Pascal, Visual Basic (derived from BASIC), Perl, COBOL 2002, PHP, Ada 95. • Languages with abstract support but without all features of object-orientation. This includes object-based and prototype- based languages. Examples: Modula-2, JavaScript, Lua. • Chameleon languages that support multiple paradigms, including OO, hybrid object system that supports both prototype-based programming and class-based OO (Tcl). Object oriented programming

• Dynamic dispatch/message passing • Inheritance • Polymorphism • Abstraction • Encapsulation

Dynamic dispatch/message passing

• by definition, it is the responsibility of the object, to 'on-demand' select the procedural code to run/execute in response to a method call, by looking up the method at run time in a table associated with the object, • multiple dispatch - if there are multiple methods that might be run for a given name (this process require some language support).

Objects and methods invocation int main() {

TComplex a, b, c;

TComplex d( 10, 20 );

std::cout << c.GetRe() << std::endl; std::cout << d.GetRe() << std::endl; } Object life cycle

• creation (constructors) • state changes of object • removal (destructor)

Initialization - constructors

• default • parameterized • copying (C++) • if defined constructor by programmer no defult constructor (must be redefined) • no return values

Constructors

• defined constructors can be public (most popular, public classes), protected, private (hardly ever e.g. in Singleton pattern, private classes) Constructors

• can’t be static • can’t be virtual Constructor (not parameterized)

Complex( void ) : fRe( 0.0 ), fIm( 0.0 ) {

} Constructor (with parameters)

Complex( double re, double im ) : fRe( re ), fIm( im ) {}

Complex( double re, double im ) { fRe = re; fIm = im; }

Complex( double re) : fRe( re ) {}

Copy constructor

Complex( const Complex & c ) : fRe( c.fRe ), fIm( c.fIm ) {}

Utilization: function parameters, initialization of object by other object

Keyword const int a = 0; const int b = 1; a = 10; // b=11; Error! class example { public: void funkcja_1(void) const; void funkcja_2(); ... }; int main() { example a; const example b; a.funkcja_1(); b.funkcja_1(); a.funkcja_2(); // b.funkcja_2(); Error! } Copy constructor

Complex c(10.0, 1.0); Complex d(c); Sequence of constructor execution

• base object-> derivative

Example: class Figure { private: int x; int y; std::string name;

public: Figure() { cout << "figure default" << endl; } Figure(int _x, int _y) : x(_x), y(_y) { cout << "figure" << endl; } void rysuj() {} };

Constructors class Square: public Figure { private: int color; int width; public: Square(int _x, int _y, int c) : Figure(_x, _y), color(c) { cout << "square" << endl;} Square(int c) : color(c) { cout << "square" << endl;} }; int main() { Square s1(1, 2, 10); Square s2(10); }

Objects removing

• Destructors (C++) ~Complex() {} • Garbage collector (Java) Destructor invocation

• By operator delete (dynamic objects) • At the end of block (static objects)

Sequence of destructor execution

~Figure() { cout << "figure deallocate" << endl; }

~Square() { cout << "square deallocate" << endl; } int main() { Square * s = new Square(1,2,10); delete s; } Destructors

~Complex() { std::cout << "destroyed" << std::endl; } main() { … Complex* complex = new Complex(10.0, 1.0); Complex c(10.0, 1.0); … delete complex; } Object oriented programming

class TComplex { private: double fRe; // the real part double fIm; // the imaginary part

public:

/////////////////////////////////////////////////////////// // The class constructors /////////////////////////////////////////////////////////// TComplex( void ) : fRe( 0.0 ), fIm( 0.0 ) {

}

TComplex( double re, double im ) : fRe( re ), fIm( im ) {}

TComplex( const TComplex & c ) : fRe( c.fRe ), fIm( c.fIm ) {} // copy constructor

// Getters / Setters double GetRe( void ) const { return fRe; } double GetIm( void ) const { return fIm; }

void SetRe( double re ) { fRe = re; } void SetIm( double im ) { fIm = im; }

~TComplex() {} };

Constructors - arrays

#include using namespace std; class Box { public: Box() { cout << "Constructor called!" <

Constructors - arrays

Constructor called! Constructor called! Constructor called! Destructor called! Destructor called! Destructor called! Objects and methods invocation public static void main(String[] args) { Complex a = new Complex(); Complex c = new Complex(10, 20); System.out.println(c.fRe); } Composition

• Objects can contain other objects in their instance variables, • For example, an object in the Employee class might contain (point to) an object of the Address class, in addition to its other own instance variables, • is used to represent "has-a" relationships: every employee has an address, so every Employee object has a place to store an Address object.

Composition class Address { private: std::string street; std::string city; int houseNumber; … }; class Employee { private: std::string name; Address address; … }; Composition public class Address { private String street; private String city; private int houseNumber; } public class Employee { private String name; private Address address; }

Inheritance

• Languages that support classes almost always support inheritance. • This allows classes to be arranged in a hierarchy that represents "is- a-type-of" relationships. • For example, class Employee might inherit from class Person. All the data and methods available to the parent class also appear in the child class with the same names. • For example, class Person might define variables "first_name" and "last_name" with method "make_full_name()". These will also be available in class Employee, and with additional variables "position" and "salary". • This technique allows easy re-use of the same procedures and data definitions, with similar real-world relationships,

Inheritance public class Person { protected String first_name; protected String last_name;

public String make_full_name() { return first_name+last_name; } } public class Employee extends Person { private String position; private int salary; } Inheritance class Person { protected: std::string first_name; std::string last_name; public: std::string make_full_name() { return first_name+last_name; } }; class Employee: public Person { private: std::string position; int salary; }; Inheritance

• Subclasses can override the methods defined by superclasses. • is allowed in some languages (this can make resolving overrides complicated), • Some languages have special support for mixins, though in any language with multiple inheritance, a mixin is simply a class that does not represent an is-a-type-of relationship. Mixins are typically used to add the same methods to multiple classes. For example, class UnicodeConversionMixin might provide a method unicode_to_ascii() when included in class FileReader and class WebPageScraper, which don't share a common parent.

Inheritance

• Abstract classes cannot be instantiated into objects, they exist only for the purpose of inheritance into other classes which can be instantiated (not abstract). In Java, the final keyword can be used to prevent a class from being subclassed. • The "open/closed principle" - classes and functions "should be open for extension, but closed for modification".

C++ inheritance public – dziedziczenie pól publicznych jako publiczne, prywatnych jako prywatne, chronionych jak chronione C++ inheritance protected - dziedziczenie pól publicznych jako chronione, prywatnych jako prywatne, chronionych jak chronione

C++ inheritance private - dziedziczenie pól publicznych jako prywatnych, prywatnych jako prywatne, chronionych jak prywatne

Multi inheritance

Example: class Drawable { void rysuj() {} }; class Figure { private: int x; int y; std::string name;

public: Figure() { cout << "figure default" << endl; } Figure(int _x, int _y) : x(_x), y(_y) { cout << "figure" << endl; } void rysuj() {} };

Multi inheritance class Square: public Figure , Drawable{ private: int color; int width; public: Square(int _x, int _y, int c) : Figure(_x, _y), color(c) { cout << "square" << endl;} }; int main() { Square s; s.rysuj(); //błąd }

Multi inheritance

class BazowaA { protected: int licznik; public: void wyswietl() { std::cout << "A::wyswietl()" << '\n'; } }; class BazowaB { protected: int licznik; public: void wyswietl() { std::cout << "B::wyswietl()" << '\n'; } }; class Klasa: public BazowaA, public BazowaB { public: void wyzeruj() { BazowaA::licznik = 0; BazowaB::licznik = 0; } }; int main() { Klasa k; k.BazowaA::wyswietl(); k.BazowaB::wyswietl(); } Multi inheritance (virtual) class Bazowa { char bufor[1024]; public: void wyswietl() {} }; class Posrednia1: public virtual Bazowa {}; class Posrednia2: public virtual Bazowa {}; class Klasa: public Posrednia1, public Posrednia2 {}; int main() { Klasa k; std::cout << sizeof(Klasa) << '\n'; k.wyswietl(); } Java – Multi inheritance Drawable { } public class Figure { } public class Square extends Figure implements Drawable { } Abstract classes public abstract class Figure { protected int position; protected String name;

public void draw() {

}

public String getName() { return name; } } Abstract Class class Figure { protected: std::string name; int position; public: virtual void draw() { }

virtual std::string getName() {

} }; Inheritance vs Composition

• The doctrine of composition over inheritance advocates implementing is-a-type-of relationships using composition instead of inheritance. • For example, instead of inheriting from class Person, class Employee could give each Employee object an internal. Some languages, do not support inheritance at all.

Encapsulation

• If a class disallows calling code from accessing internal object data and forces access through methods only, this is a strong form of abstraction or information hiding known as encapsulation. • Some languages (Java, C++ for example) let classes enforce access restrictions explicitly, for example denoting internal data with the private keyword and designating methods intended for use by code outside the class with the public keyword. • Methods may also be designed public, private, intermediate levels such as protected (which typically allows access from other objects of the same class, but not objects of a different class) or default. Encapsulation

• This facilitates code refactoring, for example allowing the author of the class to change how objects of that class represent their data internally without changing any external code, • Encourages programmers to put all the code that is concerned with a certain of data in the same class, which organizes it for easy comprehension by other programmers. • Encapsulation is often used as a technique for encouraging decoupling. Overriding public class Employee extends Person { protected String position; protected int salary;

public String make_full_name() { return last_name; } } Overriding class Employee: public Person { protected: std::string position; int salary; public: std::string make_full_name() { return last_name; } }; Overloading public class Employee extends Person { private String position; private int salary;

public String make_full_name(int parameter) { return last_name; } }

Polymorphism

, a form of polymorphism, is when calling code can be agnostic as to whether an object belongs to a parent class or one of its descendants. For example, a function might call "make_full_name()" on an object, which will work whether the object is of class Person or class Employee. This is another type of abstraction which simplifies code external to the class hierarchy. • In languages that support open recursion, object methods can call other methods on the same object (including themselves), typically using a special variable or keyword called this or self. It allows a method defined in one class to invoke another method that is defined later, in some subclass Polymorphism (java example)

Person p = new Employee("Nowak", "Jerzy"); System.out.println(p.make_full_name()); Polymorphism (C++)

• Static linkage (binding) • Dynamic linkage (binding) Polymorphism (C++) virtual int area() { cout << "Parent class area :" <

Polymorphism (C++)

Figure *shape; Rectangle rec(10,7); shape = &rec; shape->area();

Operator this void SetRe( double re ) { this->fRe = re; } void SetIm( double im ) { this->fIm = im; }