OOP Concepts Sumary
Posted on August 17, 2016 by Mauricio Ferreira
The OOP concepts can be performed in any programming language. Thus, it’s possible to program using objects in procedural languages, but with OOP languages it’s easier since it directly supports the creation and use of objects;
- i.e.: C++, SmallTalk, Java, C#
- OOP focuses on the objects that make up a program, rather than the functions / procedures and the data;
- OOP consists of creating objects and making them work together;
OOP Concepts
- Class
- Abstract definition of something, an user-type, a blueprint;
- Has States / Fields / Properties (what an object knows) and Methods / Behaviors / Member Functions (what an object does);
- Defines objects behavior and default values;
- Object
- Instance of a Class, Repository of data;
- Has a unique identity: the property of an object that distinguishes it from other objects;
- Have its states: describes the data stored in the object;
- Exhibits some well-defined behavior: follows the class’s description;
- Java: every class is a subclass of Object;
- Instantiation
- Is the way of instantiating a class to create an object;
- Leaves the object in a valid state;
- Performed by a constructor;
- Constructor
- Creates the object;
- Initializes the state of the object;
- Could be explicit or implicit;
- Ensure correct initialization of all data, automatically called at the time of the object creation;
- Java Has an empty default constructor implicit, that is lost when another explicit is defined;
- C++: By default constructor accept implicit type conversion, this characteristic is lost, defining the word explict in the constructor signature;
- Destructor / Finalizer
- Clean up an object’s state;
- Ensure correct deallocation of all resources before an object is deleted from memory;
- Java: This process is automatically done by the garbage collection, but is possible to force an object’s collection through the finalize() method;
- C++: When an object may be deleted through a pointer to its base class, the base class must declare a virtual destructor. If you don’t, the derived class’s destructor will not be called (and resources may leak);
class Vehicle { virtual void Drive(); virtual ~Vehicle() {} } class Car : public Vehicle { virtual void Drive() { … } ~Car () { delete[] wheels; } } Vehicle* v = new Car(); delete v;
- Method
- Is a function that has an implicit parameter “this”;
- Is a subroutine (or procedure) associated with a class;
- Must be an operation that receives and leaves a valid state;
- Abstracts a high-level process; shields (protects, encapsulates) the user from the details of the class’s data structures;
- Define the behavior to be exhibited by instances of the associated class at program runtime;
- Have access to data stored in an instance of the class;
- The association between class and method is called binding;
- Methods can be bound to a class at compile time (static binding);
- Methods can be linked to an object at runtime (dynamic binding);
- Base class
- aka.: Base class, Parent class, Superclass;
- Other classes can inherit from a base class;
- Derived class
- aka.: Derived class, Child Class, Subclass;
- Derived class inherit the structure and behavior of the base class;
- A Derived class can modify or augment a superclass in at least three ways:
- It can declare new fields;
- It can declare new methods;
- It can override old methods with new implementations;
- Subclass can have subclasses; Subclassing is transitive;
- A Derived class constructor executes first the superclass constructor;
- Java: The zero-parameter constructor is always called by default. To change this default behavior, must call the constructor super(…). The super keyword must be the first statement in the subclass constructor;
- Is-a vs. Has-a relationships
- To favor composition over inheritance is a design principle that gives the design higher flexibility, providing business-domain classes and more stable business domain in the long term. In other words, HAS-A can be better than an IS-A relationship
- Interface
- aka: Protocol;
- Declares a set of related methods, outside of any class;
- Ultimate encapsulators (Hide details of a class);
- C++: There is not separate concept of interface, it is simulated by using abstract classes with all pure virtual methods; (and different from abstract class)
- Abstract Class
- Is a class whose sole purpose is to be extended;
- Lets you define an interface: for multiple classes to share, without setting any of them yet;
- Incomplete class definition that declares but not define all of its methods;
- Is a proper Class, but cannot be instantiated;
- C++: Pure virtual functions;
- Virtual Method
- Virtual methods are the means by which a class object can achieve polymorphic behavior. Non-virtual methods, or regular methods, are those who do not participate in polymorphism
- Run-time based (advantage);
- Used for polymorphism (advantage);
- Takes longer to invoke virtual method, and extra memory is required to store the information needed for the lookup (disadvantage);
- Java: Non-Static methods are always virtual;
- C++: To define a virtual method, the VIRTUAL keyword must be used;
- Pure Virtual method / Abstract Method
- An abstract method is one with only a signature and no implementation body.
- It is often used to specify that a subclass must provide an implementation of the method.
- Abstract methods are used to define interfaces in some computer languages
- Static Method
- aka.: Class Method;
- Doesn’t implicitly take an object as a parameter;
- Doesn’t have “this”;
- Java: Main() is always static;
- “versus” instance methods;
- Friend Method
- A function that has access to the private members of a class but is not itself a member of the class. An entire class can be a friend of another class.
- Static Field
- aka.: Class Variables;
- A single variable shared by a whole class of objects;
- Global variables;
- “versus” instance fields;
- Static / Class Initializer
- A class’s static initialization typically happens at before main at run-time;
- Good use to Singleton (?)
- Dynamic Binding
- Dynamic binding means that the code associated with a given procedure is not known until the time of the call at run time
- Method Overriding
- Means having different implementation of the same method in the inherited class, same signature, same name;
- Invoking overridden methods: it could be called by super.method (Java) or Class::method (C++), and it’s not needed to be the first statement in a method;
- When invoking an overridden method of an object whose dynamic type is different from its static type:
- If the method is non-virtual, the static type’s method is invoked;
- If the method is virtual, the dynamic type’s method is invoked;
- Method Overloading
- Are those that appear to have the same name, but that have different formal parameter types (or result from the value type, if the language supports overloading on result type).
- The main difference between Method Overloading and Method Overriding is that the choice of a method from a set of overriding methods is made according to the class of the receiver of the method request, whereas the name of an overloaded method is constructed according to the static types of the arguments to the method request. Another difference is that an overriding method must be declared in a subclass of the class that declared the overridden method, while several overloadings of a method name can be declared in the same class.
- Composition x Aggregation
- Aggregation implies a relationship where the child can exist independently of the parent. Example: Class (parent) and Student (child). Delete the Class and the Students still exist.
- Composition implies a relationship where the child cannot exist independent of the parent. Example: House (parent) and Room (child). Rooms don’t exist separate to a House.
- Method Signatures
- Return value, name, and parameters;
- Method visibility
- i.e.: Public / Private / Protected / Package
- Private Keyword
- Private method or a field has the property to be invisible and inaccessible to other classes;
- To prevent data to be corrupted by other classes (keep vulnerable parts private);
- Can improve the implementation without causing other classes to fail;
- Huge advantage to OOP;
- Public Keyword
- Accessible to every class;
- Protected Keyword
- Are always private, except to the derived classes;
- Package (only Java)
- Get and Set Terminology
- accessor = getter = getX();
- mutator = setter = setX();
- Persistence
- Saves the state and class of an object across time and space;
- Java: Object serialization
- Modularity
- Abstractions into a discrete units;
- Classes / Packages / Domains;
- Call by reference vs. Call by value
- Call by reference: A function call mechanism that passes arguments to a function by passing the addresses of the arguments;
- Call by value: A function call mechanism that passes arguments to a function by passing a copy of the value of the arguments