Posts

Showing posts with the label OOP

Examples of basic design patterns

Image
Delegation pattern - An entity externally expresses some behavior, but in reality transfers responsibility for performing this behavior to a related object. Functional design - Ensures that each entity has only one responsibility and performs it with a minimum of side effects on others. Immutable interface - Creation of an immutable object. Interface - A general method for structuring entities to make them easier to understand. Marker interface - The presence or absence of an implementation of the marker interface is used as an attribute (as an entity tag). In modern programming languages, attributes or annotations are used instead. Property container - Allows you to add additional entity properties to the container within itself, instead of expanding with new properties. Event channel - Creates a centralized channel for events. Uses a proxy to subscribe and a proxy to post an event to a channel. The Representative is separate from the real publisher or subscriber. A subs...

Types of design patterns

Image
Fundamental are the basic building blocks of other templates. Most of the other templates use these templates in one form or another. Creational patterns are design patterns that abstract the instantiation process. They allow you to make the system independent of the way objects are created, composed and presented. The parenting template uses inheritance to modify the generated object, and the parenting template delegates the creation of objects to another object. Structural templates define various complex structures that change the interface of an existing object or its implementation, making it easier to develop and optimize a program. Behavioral patterns define the interaction between objects, thus increasing its flexibility. Read also: Where and what is the abstract modifier used for What is a Design Pattern Concept of "interface" in Java

What is a Design Pattern

Image
The design pattern is a proven and ready-to-use solution. This is not a class or a library that can be connected to the project, it is something more - it does not depend on the programming language, it is not a complete sample that can be directly converted into code and can be implemented in different ways in different programming languages. Pros of using templates: reducing the complexity of development due to ready-made abstractions for solving a whole class of problems. facilitating communication between developers by allowing reference to known patterns. unification of solution details: modules and project elements. the opportunity to find a good solution, use it again and again. assistance in choosing the most suitable design option. Minuses: blindly following some chosen pattern can complicate the program. a desire to try a certain pattern in action without any particular reason. What are the main characteristics of templates Name - all templates have a unique ...

What are the access modifiers in Java

Image
private : the members of the class are only accessible within the class. The service word private is used for designation. default, package-private, package level (package level access): Visibility of the class/class members only inside the package. This is the default access modifier - no special designation is required. protected : class members are available inside the package and in descendants. The service word protected is used for designation. public : the class/members of the class are available to everyone. The service word public is used for designation. A sequence of modifiers in ascending order of closeness: public, protected, default, private. During inheritance, it is possible to change access modifiers towards greater visibility (to maintain compliance with Barbara Liskov's substitution principle). Read also: How are JRE, JVM and JDK different Frames in JVM Execution Engine in JVM

What are static and dynamic binding

Image
Attaching a method call to the body of a method is called binding. If the binding is done by the compiler (linker) before starting the program, then it is called static or early binding . In turn, late binding is binding carried out directly at runtime, depending on the type of object. Late binding is also called dynamic or runtime binding . In languages ​​that implement late binding, there must be a mechanism for determining the actual type of an object at runtime to invoke the appropriate method. In other words, the compiler does not know the type of the object, but the method invocation mechanism determines it and calls the corresponding method body. Late-binding is language-specific, but it's not hard to assume that some additional information must be included in objects to implement it. All Java methods use late (dynamic) binding, unless the method has been declared final (private methods are final by default). Read also: Basic principles of OOP What is "encapsula...

What's the difference between composition and aggregation

Image
What do the expressions “is” and “has” mean in terms of OOP principles? "Is" implies inheritance. "Has" means an association (aggregation or composition). What's the difference between composition and aggregation? Association denotes a relationship between objects. Composition and aggregation are special cases of the “part-whole” association. Aggregation assumes that objects are connected in a "part-of" relationship. Composition is a stricter variant of aggregation. In addition to the “part-of” requirement, the condition is imposed that the “part” instance can only be included in one whole (or not included anywhere), while in the case of aggregation, the “part” instance can be included in several ones. For example, a book consists of pages, and we cannot rip a page out of a book and insert it into another book. Pages are clearly linked to a specific book, so this is composition. At the same time, we can take and transfer a book from one library to...

What are the advantages and disadvantages of an object-oriented approach to programming

Image
Benefits : The object model is quite natural, since it is primarily focused on the human perception of the world, and not on computer implementation. Classes allow you to construct from useful components with simple tools that abstract away from implementation details. Data and operations on them form a definite entity, and they are not spread throughout the program, as is often the case in procedural programming, but are described together. Localization of code and data improves the visibility and maintainability of software. Encapsulation allows you to introduce modularity, which makes it easier to parallelize the execution of a task across multiple workers and update versions of individual components. The ability to create extensible systems. Using polymorphism is useful when: Processing heterogeneous data structures. Programs can work without distinguishing the type of objects, which greatly simplifies the code. New species can be added at any time. Changes in behavior du...

Basic concepts of OOP: "class", "object", "interface"

Image
A class is a way to describe an entity, defining the state and behavior that depends on this state, as well as the rules for interacting with this entity (contract). From a programming point of view, a class can be viewed as a set of data (fields, attributes, class members) and functions for working with them (methods). From the point of view of program structure, a class is a complex data type. An object (instance) is a separate representative of a class with a specific state and behavior that is completely determined by the class. Each object has specific attribute values ​​and methods that operate on those values ​​based on the rules defined in the class. An interface is a collection of class methods available for use. The interface of a class will be a set of all its public methods together with a set of public attributes. Basically, an interface specifies a class, clearly defining all possible actions on it. Read also: Basic principles of OOP What is "encapsulatio...

What is messaging in OOP

Image
Objects interact by sending and receiving messages. A message is a request to perform an action, followed by a set of arguments that may be needed when performing an action. In OOP, sending a message (calling a method) is the only way to transfer control to an object. If an object is to "reply" to this message, then it must have a method corresponding to the message. Also, objects, using their methods, can themselves send messages to other objects. Messaging is done with dynamic calls, resulting in extreme late binding. Let's say you want to create a physical model describing colliding balls of different sizes. The traditional approach to solving this problem is approximately the following: a set of data describing each ball (for example, its coordinates, mass and acceleration) is determined; each ball is assigned a unique identifier (for example, an array is organized, the index value of which corresponds to the ball number), which will distinguish each of the balls fro...

What is "abstraction" in OOP

Image
Abstraction is a way to highlight a set of general characteristics of an object, excluding private and insignificant ones from consideration. Accordingly, abstraction is a collection of all such characteristics. Imagine that a driver is driving through a busy traffic area. It is clear that at this moment he will not think about the chemical composition of the paint of the car, the peculiarities of the interaction of the gears in the gearbox or the influence of the body shape on the speed (unless the car is in a dead traffic jam and the driver has absolutely nothing to do). However, he will use the steering wheel, pedals, turn signal regularly. Example: // Abstract class abstract class Animal { // Abstract method (does not have a body) public abstract void animalSound(); // Regular method public void sleep() { System.out.println("Zzz"); } } // Subclass (inherit from Animal) class Pig extends Animal { public void animalSound() { ...

What is "polymorphism" in OOP

Image
Polymorphism is the property of a system to use objects with the same interface without information about the type and internal structure of the object. The advantage of polymorphism is that it helps reduce program complexity by allowing the same interface to be used to define a single set of actions. The choice of a specific action, depending on the situation, rests with the compiler of the programming language. Hence the key feature of polymorphism is the use of an object of a derived class, instead of an object of the base class (descendants can change their parent behavior, even if they are accessed by reference of the parent type). Any training in driving would not make sense if a person who learned to drive, say, a VAZ 2106 could not then drive a VAZ 2110 or BMW X3. On the other hand, it is difficult to imagine a person who could normally drive a car in which the gas pedal is to the left of the brake pedal, and instead of the steering wheel there is a joystick. The thing is t...

What is "inheritance" in OOP

Image
Inheritance is a property of the system that allows you to describe a new class based on an existing one with partially or completely borrowed functionality. The class from which inheritance is made is called ancestor, base or parent. The new class is a descendant, inheritor, or derived class. Let's imagine ourselves, for a moment, engineers of an automobile plant. Our task is to develop a modern car. We already have a previous model that has proven itself over the years. Everything would be fine, but times and technologies are changing, and our modern plant should strive to improve the convenience and comfort of products and meet modern standards. We need to make a whole range of cars: sedan, station wagon and subcompact hatchback. Obviously, we are not going to design a new car from scratch, but, taking the previous generation as a basis, we will make a number of design changes. For example, let's add power steering and reduce the gaps between the fenders and the hood lid...

What is "encapsulation"

Image
Encapsulation is a property of the system that allows you to combine data and methods that work with it in a class and hide implementation details from the user, revealing only what is needed for subsequent use. The goal of encapsulation is to get away from the implementation dependency of the external interface of a class (something that other classes can use). So that the slightest change in the class does not entail a change in the external behavior of the class. Let's imagine for a moment that we found ourselves at the end of the century before last, when Henry Ford had not yet invented the conveyor belt, and the first attempts to create a car faced criticism from the authorities that these smoking monsters pollute the air and scare horses. Imagine that in order to drive the first steam car, it was necessary to know how the steam boiler works, constantly toss up coal, monitor the temperature and water level. In this case, to turn the wheels, use two levers, each of which tur...

Basic principles of OOP

Image
Basic principles of object-oriented programming (OOP) are follows: Encapsulation is implementation hiding. Inheritance is the creation of a new entity based on an existing one. Polymorphism is the ability to have different forms for the same entity. Abstraction is a set of common characteristics. Sending messages is a form of communication, interaction between entities. Reuse - everything listed above works for code reuse. This is the only correct order of OOP paradigms, since each subsequent one uses the previous ones. Read also: What is object-oriented programming

What is object-oriented programming

Image
Object-oriented programming (OOP) is a programming methodology based on representing a program as a collection of objects, each of which is an instance of a certain class, and the classes form a hierarchy of inheritance. Object Oriented Programming uses objects rather than algorithms as the main logical building blocks; every object is an instance of a certain class classes form hierarchies. A program is considered object-oriented only if all three of these requirements are met. In particular, programming that does not use inheritance is called programming using abstract data types, rather than object-oriented. According to the OOP paradigm, a program consists of objects that exchange messages. Objects can have state, the only way to change the state of an object is to send it a message, in response to which the object can change its own state.