Open anitsh opened 4 years ago
The style of programming which is largely based on building data types is known as Object-Oriented Programming
, as it revolves around the concept of an Object
, an entity that holds a data type value. Programs are treated as a set of interacting objects in OOP.
Software design might be classified in many different architectures. Imperative programming uses statements that change a program's state. Imperative programming focuses on describing how a application operates. Object-oriented Programming groups instructions together with the part of the state they operate on.
In OOP world, everything is an is an object with some properties and behavior and in some relationship other objects.
OOP is a Programming Paradigm
which is used to design computer programs by making them out of Objects
, their Domains
and how those objects interact with one another in their domain and with the outside world.
'Domain Model is an object model of the domain that incorporates both behavior and data' - Martin Fowler
All the material things that can be seen and touched are the real world objects. Software objects are instances of templates that are modeled based on the domain of problem having unique characteristics based on the States
and Behaviors
. An object stores its state in fields
(variables
in some programming languages) and exposes its behavior through methods
(functions
in some programming languages).
These contain data and functions bundled together under a unit. In other words class is a collection of similar objects. When we define a class it just creates a template or skelton. So no memory is created when class is created. Memory is occupied only by the object.
Key characteristics of OOP.
The process of expressing or showing the most important facts about something
- Dictionary
Similarly in programming, only those functionalities are exposed which are useful to interact with the instance of the object.
A Class
is used to encapsulate attributes and behaviors and only exposes a few public methods.
Existing as an idea, feeling, or quality, not as a material object; in general
- Dictionary
Similarly in programming, it means to provide just an general idea of the behaviors of the object.
Abstract Class
and Interface
are used to express the intent of the Class
rather than the actual implementation. In a way, one class should not know the inner details of another in order to use it, just knowing the interfaces should be good enough.
A physical or mental characteristic inherited from your parents
- Dictionary
Similarly in programming, an object can inherit behaviors of other objects. It is achieved via Class Inheritance or Interface Inheritance.
Inheritance is based on IS-A relationship which means an object is a type of another. For example, Apple is a Fruit, Car is a Vehicle etc. Inheritance is uni-directional. For example, House is a Building. But Building is not a House.
The fact that something such as an animal or organism can exist in different forms
- Dictionary
Similarly in programming, object's behaviors can be modified as required via Overloading
and Overriding
.
The parts, substances, etc. that something is made of
- Dictionary
Similarly in programming, an object can be made of many other objects. It is based on HAS-A relationship
OOAD is a technical approach for analyzing and designing an application, system, or business in OOP, as well as using visual modeling throughout the software development process to guide stakeholder communication and product quality. OOAD uses Unified Modeling Language (UML)
is used as a tool to provide a standard way to visualize the design of a system.
UML is a common language for business analysts, software architects and developers used to describe, specify, design, and document existing or new business processes, structure and behavior of artifacts of software systems.
OOAD in modern software engineering is typically conducted in an iterative and incremental way. The outputs of OOAD activities are analysis models for Object Oriented Analysis (OOA) and design models for Object Oriented Design (OOD), respectively. The intention is for these to be continuously refined and evolved, driven by key factors like risks and business value.
Class Diagram
is structure diagram which shows structure of the designed system at the level of classes and interfaces, shows their features, constraints and relationships - associations, generalizations, dependencies, etc.
During OOA we ask two questions to create a domain model of the objects:
Object be in
? Object perform
?A Domain Model
is a conceptual model of the domain that incorporates both behavior and data which is an important part of OOAD. `
Domain Modelingis a way to describe and model real world entities and their relationships between them, which collectively describe the
Problem Domain Space. Derived from an understanding of
System-level Requirements,
Identifying Domain Entitiesand
Their Relationshipsprovides an effective basis for understanding and helps practitioners
Design Systemsfor
Maintainability,
Testability, and
Incremental Development`. Because there is often a gap between understanding the problem domain and the interpretation of requirements, domain modeling is a primary modeling tool in OOAD driven development.
Domain modeling envisions the solution as a set of domain objects that collaborate to fulfill system-level scenarios. As the system design changes, refactoring and updating the domain model is vital to maintaining a continuing understanding of the system, and goes hand in hand with code refactoring to help control the inherent complexity of software systems.
https://www.uml-diagrams.org/class-diagrams-overview.html
Encapsulation is a technique to hide internal state of an object and requiring all interaction to be performed through the object's methods. This is a security mechanism to protect the data the object carries which are represented as their state, so also called Data Encapsulation
. A Class
is designed to encapsulate the variables and made only accessible through methods.
Abstraction is a technique to provide an easy way to access the behaviors of some object without knowing who or which it was and how it works internally. Interfaces
enables this feature in OOP which is used widely. Abstract Class
also provides abstraction but with some limitations.
Polymorphism is the modeling technique that allows to create a single interface
to various operands, arguments, and objects. The whole concept of Interface
is build around the idea of polymorphism.
Inheritance provides a powerful and natural mechanism for organizing and structuring your software.
Bundling code into individual software objects provides a number of benefits, including:
In object-oriented programming, code is organized into objects that contain a state that is only modified by the code that is part of the object.
A Class
is a blueprint
or prototype
from which objects are created.
Example of a class and it's implementation in Java Programming. The following is Bicycle class with three states - cadence
, speed
, gear
, and three behaviors - changeCadence
, changeGear
and printStates
. And a BicycleDemo class that creates two separate Bicycle objects and invokes their methods.
class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
void changeCadence(int newValue) {
cadence = newValue;
}
void changeGear(int newValue) {
gear = newValue;
}
void speedUp(int increment) {
speed = speed + increment;
}
void applyBrakes(int decrement) {
speed = speed - decrement;
}
void printStates() {
System.out.println("cadence:" +
cadence + " speed:" +
speed + " gear:" + gear);
}
}
class BicycleDemo {
public static void main(String[] args) {
// Create two different Bicycle objects
Bicycle bike1 = new Bicycle();
Bicycle bike2 = new Bicycle();
// Invoke methods on those objects
bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
}
}
Classes should be immutable unless there's a very good reason to make them mutable.... If a class cannot be made immutable, you should still limit its mutability as much as possible.
- Joshua Bloch (Java architect)
A layman definition of Interface
is 'A point where two systems, subjects, organizations, etc. meet and interact'
. In general software world, objects define their interaction with the outsideworld through the methods that they expose as interfaces to the outside world. To put it simply, an interface is a contract.
In object oriented programming, interface is used as very important part in the object-oriented design technique that leads to good object-oriented software design to build more understandable, flexible and maintainable system.
**Program to an interface, not an implementation**
**Composition over inheritance: Favor object composition over class inheritance**
Above two are the most widely used object orient techniques which has interface implementation at their core. These techniques were introduced and described in details in the book Design Patterns: Elements of Reusable Object-Oriented Software (1994)
by the Gang Of Four
.
**SOLID**
design principles are five most popular good software design principles which also has interface foundation for better design.
Interfaces should be looked at more of a concept and not just as a keyword
to understood it's better usage and the holistic value it adds in the system design. Even in the language which does not have interface
keywordlike JavaScript and Python, it is still possible to implement
Program To An Interface` technique.
To quote from the Gang of Four’s design patterns book, using interfaces means the following:
Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If the class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
Technically, An interface is a group of related methods with empty bodies. More than one interfaces can be implemented to define the behavior of an object. In Java programming: All fields are automatically public, static, and final, and all methods are public. Methods in an interface that are not declared as default or static are implicitly abstract, so the abstract modifier
is not used with interface methods, it can be used, but it is unnecessary. Interfaces are implemented using implements
keyword.
A bicycle's behavior, if specified as an interface, might appear as follows:
interface Bicycle {
// wheel revolutions per minute
void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}
class ACMEBicycle implements Bicycle { //Interface Implementation
int cadence = 0;
int speed = 0;
int gear = 1;
// The compiler will now require that methods
// changeCadence, changeGear, speedUp, and applyBrakes
// all be implemented. Compilation will fail if those
// methods are missing from this class.
void changeCadence(int newValue) {
cadence = newValue;
}
void changeGear(int newValue) {
gear = newValue;
}
void speedUp(int increment) {
speed = speed + increment;
}
void applyBrakes(int decrement) {
speed = speed - decrement;
}
void printStates() {
System.out.println("cadence:" +
cadence + " speed:" +
speed + " gear:" + gear);
}
}
Object-oriented programming allows classes to inherit
commonly used state and behavior from other classes.
An example of inheritance in Java programming.
class MountainBike extends Bicycle {
// new fields and methods defining
// a mountain bike would go here
}
Here MountainBike is inheriting Bicycle class. This gives MountainBike all the same fields and methods as Bicycle, yet allows its code to focus exclusively on the features that make it unique.
An abstract class is a class that is declared with keyword abstract
. It may or may not include abstract methods. Abstract classes cannot be instantiated
, but they can be sub-classed like inheritance and may contain a mix of methods declared with or without an implementation. In an abstract classes, we can declare fields that are not static and final, and define public, protected, and private concrete methods.
An abstract method is a method that is declared without an implementation. If a class includes abstract methods, then the class itself must be declared abstract.
// Java Example
public abstract class GraphicObject {
// declare fields
// declare nonabstract methods
abstract void draw();
}
When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.
An example of an abstract class in the JDK is AbstractMap, which is part of the Collections Framework. Its subclasses (which include HashMap, TreeMap, and ConcurrentHashMap) share many methods (including get, put, isEmpty, containsKey, and containsValue) that AbstractMap defines.
An example of a class in the JDK that implements several interfaces is HashMap, which implements the interfaces Serializable, Cloneable, and Map<K, V>. By reading this list of interfaces, you can infer that an instance of HashMap (regardless of the developer or company who implemented the class) can be cloned, is serializable (which means that it can be converted into a byte stream; see the section Serializable Objects), and has the functionality of a map. In addition, the Map<K, V> interface has been enhanced with many default methods such as merge and forEach that older classes that have implemented this interface do not have to define.
Note that many software libraries use both abstract classes and interfaces; the HashMap class implements several interfaces and also extends the abstract class AbstractMap.
Object Oriented
The core idea of OOP is based on objects and how they communicate.
Code is organized into objects that contain a state that is only modified by the code that is part of the object.
Object Oriented Programming (OOP)
The style of programming which is largely based on building data types is known as
Object-Oriented Programming
, as it revolves around the concept of anObject
, an entity that holds a data type value. Programs are treated as a set of interacting objects in OOP.Software design might be classified in many different architectures. Imperative programming uses statements that change a program's state. Imperative programming focuses on describing how a application operates. Object-oriented Programming groups instructions together with the part of the state they operate on.
In OOP world, everything is an is an object with some properties and behavior and in some relationship other objects.
OOP is a
Programming Paradigm
which is used to design computer programs by making them out ofObjects
, theirDomains
and how those objects interact with one another in their domain and with the outside world.'Domain Model is an object model of the domain that incorporates both behavior and data' - Martin Fowler
Object
All the material things that can be seen and touched are the real world objects. Software objects are instances of templates that are modeled based on the domain of problem having unique characteristics based on the
States
andBehaviors
. An object stores its state infields
(variables
in some programming languages) and exposes its behavior throughmethods
(functions
in some programming languages).Class
These contain data and functions bundled together under a unit. In other words class is a collection of similar objects. When we define a class it just creates a template or skelton. So no memory is created when class is created. Memory is occupied only by the object.
Does Object Makes A Language Object-oriented?
Key characteristics of OOP.
Encapsulation
The process of expressing or showing the most important facts about something
- Dictionary Similarly in programming, only those functionalities are exposed which are useful to interact with the instance of the object.A
Class
is used to encapsulate attributes and behaviors and only exposes a few public methods.Abstraction`
Existing as an idea, feeling, or quality, not as a material object; in general
- Dictionary Similarly in programming, it means to provide just an general idea of the behaviors of the object.Abstract Class
andInterface
are used to express the intent of theClass
rather than the actual implementation. In a way, one class should not know the inner details of another in order to use it, just knowing the interfaces should be good enough.Inheritance
A physical or mental characteristic inherited from your parents
- Dictionary Similarly in programming, an object can inherit behaviors of other objects. It is achieved via Class Inheritance or Interface Inheritance.Inheritance is based on IS-A relationship which means an object is a type of another. For example, Apple is a Fruit, Car is a Vehicle etc. Inheritance is uni-directional. For example, House is a Building. But Building is not a House.
Polymorphism
The fact that something such as an animal or organism can exist in different forms
- Dictionary Similarly in programming, object's behaviors can be modified as required viaOverloading
andOverriding
.Composition
The parts, substances, etc. that something is made of
- Dictionary Similarly in programming, an object can be made of many other objects. It is based on HAS-A relationshipObject Oriented Analysis And Design (OOAD)
OOAD is a technical approach for analyzing and designing an application, system, or business in OOP, as well as using visual modeling throughout the software development process to guide stakeholder communication and product quality. OOAD uses
Unified Modeling Language (UML)
is used as a tool to provide a standard way to visualize the design of a system.UML is a common language for business analysts, software architects and developers used to describe, specify, design, and document existing or new business processes, structure and behavior of artifacts of software systems.
OOAD in modern software engineering is typically conducted in an iterative and incremental way. The outputs of OOAD activities are analysis models for Object Oriented Analysis (OOA) and design models for Object Oriented Design (OOD), respectively. The intention is for these to be continuously refined and evolved, driven by key factors like risks and business value.
Class Diagram
is structure diagram which shows structure of the designed system at the level of classes and interfaces, shows their features, constraints and relationships - associations, generalizations, dependencies, etc.During OOA we ask two questions to create a domain model of the objects:
Object be in
?Object perform
?Domain Model
A
Domain Model
is a conceptual model of the domain that incorporates both behavior and data which is an important part of OOAD. `Domain Modeling
is a way to describe and model real world entities and their relationships between them, which collectively describe the
Problem Domain Space. Derived from an understanding of
System-level Requirements,
Identifying Domain Entitiesand
Their Relationshipsprovides an effective basis for understanding and helps practitioners
Design Systemsfor
Maintainability,
Testability, and
Incremental Development`. Because there is often a gap between understanding the problem domain and the interpretation of requirements, domain modeling is a primary modeling tool in OOAD driven development.Domain modeling envisions the solution as a set of domain objects that collaborate to fulfill system-level scenarios. As the system design changes, refactoring and updating the domain model is vital to maintaining a continuing understanding of the system, and goes hand in hand with code refactoring to help control the inherent complexity of software systems.
https://www.uml-diagrams.org/class-diagrams-overview.html
Four Main Conceptual Pillars Of Object Oriented Programming
Encapsulation is a technique to hide internal state of an object and requiring all interaction to be performed through the object's methods. This is a security mechanism to protect the data the object carries which are represented as their state, so also called
Data Encapsulation
. AClass
is designed to encapsulate the variables and made only accessible through methods.Abstraction is a technique to provide an easy way to access the behaviors of some object without knowing who or which it was and how it works internally.
Interfaces
enables this feature in OOP which is used widely.Abstract Class
also provides abstraction but with some limitations.Polymorphism is the modeling technique that allows to create a
single interface
to various operands, arguments, and objects. The whole concept ofInterface
is build around the idea of polymorphism.Inheritance provides a powerful and natural mechanism for organizing and structuring your software.
Benefits Of Using Objects
Bundling code into individual software objects provides a number of benefits, including:
In object-oriented programming, code is organized into objects that contain a state that is only modified by the code that is part of the object.
Resource
Inheritance VS Composition
Polymorphism
Encapsulation
Interface
Foundations for Object Oriented thinking
Resource