This is the 4th article on the System Design and Software Architecture series. In this article, we are discussing the Core objectives and components of Object-Oriented Programming  is an important section in System design and architecture

Previous articles

What is Object-Oriented programming (OOP)

What is Object-Oriented programming (OOP)
What is Object-Oriented programming (OOP)

Object-oriented programming (OOP) is computer programming (software development). It defines the data type of a data structure and the types of operations or functions that can apply to the data structure.

From this way, the data structure becomes an object that contains both data and functions. Also, programmers can make connections between one object and another.

OOP focuses on the objects that developers need to manipulate rather than the logic that they need to manipulate. This programming approach is well suited for programs that are large, complex, and programs that are actively updated or maintained.

This method is useful for the development of collaborations divided into project groups by organizing an object-oriented program.

Characteristics of OOP

Polymorphism:

Polymorphism
Polymorphism

Polymorphism is the ability to efficiently distinguish between OOPs programming languages ​​with the same name. Java does this with the help of the signatures and publications of these organizations.

There are mainly two types of Polymorphism in Java:

Runtime Polymorphism (Override)

Runtime polymorphism is also known as dynamic polymorphism or late binding. In runtime polymorphism, the function call is resolved at run time. 

Example

Lets create Vehicle.java class with tyreCount Function to print count of tires.

@Slf4j
public class Vehicle {
    public void tyreCount() {
        log.info("How may tires have?");
    }
}

Let’s create Car.java class and extend it from Vehicle.java class. then we print the tyre count.

@Slf4j
public class Car extends Vehicle {
    public static void main(String[] args) {
        Vehicle vehicle = new Car();
        vehicle.tyreCount();
    }

    @Override
    public void tyreCount() {
        log.info("4 tyres");
    }
}

output

> Task :Car.main()
4 tyres

and another Truck.java class and extend it from Vehicle.java class. then we print the tyre count.

@Slf4j
public class Truck extends Vehicle {
    public static void main(String[] args) {
        Vehicle vehicle = new Truck();
        vehicle.tyreCount();
    }

    @Override
    public void tyreCount() {
        log.info("10 tyres");
    }
}

outout

> Task :Truck.main()
10 tyres
Compile-time Polymorphism (Overloading )

Compile-time Polymorphism (or Static polymorphism). A polymorphism that is resolved during compiler time is known as static polymorphism. 

To overload a method there are three ways,

To overload a method, the argument lists of the methods must differ in either of these:
1. Number of method parameters.
Example

printValues(int i, int j)
printValues(int i, int j, int k)

2. Data type of method parameters.
Example:

printValues(int i, int j)
printValues(int i, float j)

3. Sequence of Data type of method parameters.
Example:

printValues(float i, int j)
printValues(int i, float j)

Method overloading is done by declaring the same method with different parameters. The parameters must be different in either of these: number, sequence or types of parameters (or arguments). Let’s see examples of each of these cases.

Example

@Slf4j
public class PrintValues {
    public void printValues(int i, int j){
        log.info("{} {}",i, j);
    }
    public void printValues(int i, int j, int k){
        log.info("{} {} {}", i, j, k);
    }
    public void printValues(int i, float j){
        log.info("{} {}", i, j);
    }
    public void printValues(float i, int j){
        log.info("{} {}", i, j);
    }
}

Lets Test the methods

class PrintValuesTest {
    PrintValues printValues = new PrintValues();

    @Test
    void testPrintValues() {
        printValues.printValues(10, 11);
    }

    @Test
    void testPrintValues2() {
        printValues.printValues(10, 11, 12);
    }

    @Test
    void testPrintValues3() {
        printValues.printValues(10, 11f);
    }

    @Test
    void testPrintValues4() {
        printValues.printValues(10f, 11);
    }
}

outout

- 10 11
- 10 11 12
- 10 11.0
- 10.0 11

We can see all function properly working without compile time errors.

Inheritance: 

Inheritance
Inheritance

This is an important pillar of OOP (Object Oriented Programming). It is the Java mechanism that allows one class to inherit another class of features or fields and methods. 

Important Glossary:

Super Class: The class that inherits features is called the superclass or elementary class or parent class.

Subclass: The class that inherits the other class is called the subclass (or derivative class, long class, or children’s class). The subclass can add its fields and methods in addition to the superclass fields and methods.

Ability to reuse: Inheritance supports the concept of “reuse”, that is when we want to create a new class and already have a class that contains some of the code we need, our new class that can obtain from the existing class. By doing this, we reuse the fields and methods of the existing class.

Let’s see examples for Inheritance

Example

Super class User.java

@Data
public class User {
    private String username;
    private String firstName;
    private String lastName;
}

Sub Class AdminUser.java which extend from above Super class

@Data
@ToString(callSuper=true)
public class AdminUser extends User{
    private String organization;
    private String department;
}

Lets Test the toString method on Sub class and see the output.

Here we are using Podam to fill the object with test values. You can learn more about Podam Usage on Java Unit Tests make easy – Random Values with PODAM (onloadcode.com)

class AdminUserTest {

    @Test
    void testToString(){
        PodamFactory factory = new PodamFactoryImpl();
        AdminUser adminUser = factory.manufacturePojo(AdminUser.class);
        String result = adminUser.toString();
        System.out.println(result);
    }
}

Output

AdminUser(super=User(username=VSnd9H90jp, firstName=Ve7_tUPPw1, lastName=jNrxPW480x), organization=MHnHjdo_9n, department=Zhy4xPkZmE)

Here we can see all the fields on both classes.

Encapsulation:

Encapsulation
Encapsulation

Encapsulation is defined as the wrapping of data under a single unit. It is the mechanism that binds the code and the data it handles together. Another way to think about circulation is to use a security shield that prevents data from entering the code outside the shield.

In technical locking, class variables or data are hidden from any other class and it can only access through any member function of their class that has published them.

As in encryption, it is also called data hiding because the data in a class is hidden from other classes.

Circulation can achieve by declaring all the variables in the class as private and by writing common class methods to set and retrieve the values ​of the variables.

Example

Lets create a calls InterestCalculator.java class to calculate simple Interest.

public class InterestCalculator {

  /** Private fields. */
  public float principalAmount;
  public float interestRate;
  public float noOfPeriods;

  /** constructor to initialize values */
  public InterestCalculator(float principalAmount, float interestRate, float noOfPeriods) {
    this.principalAmount = principalAmount;
    this.interestRate = interestRate;
    this.noOfPeriods = noOfPeriods;
  }

  /** method to calculate Simple interest and return interest in float values . */
  public float simpleInterest() {
    return (principalAmount * interestRate * noOfPeriods) / 100;
  }
}

In this code snippet, we can see all filed have private access. so we cant access them directly outside from this class,

Lets test this

class InterestCalculatorTest {
  InterestCalculator interestCalculator = new InterestCalculator(13000, 12, 2);

  @Test
  void testSimpleInterest() {
    float result = interestCalculator.simpleInterest();
    Assertions.assertEquals(3120.0, result);
  }
}

Summary: Data Summary is a property that shows only essential information to the user. Minor or non-essential units are not displayed to the user. As an example, a car is considered a car rather than its parts.

Abstraction:

Abstraction
Abstraction

Data abstraction can define as the process of identifying only the required properties of an object. And it is ignoring irrelevant information. The properties and behaviour of an object distinguish it from other similar types of objects and also help to classify or group objects.

Example

Lets Create abstract class Shape.java and extend two classes from that class.

abstract class Shape {
  abstract void getShapeName();

  public void whoAmI() {
    System.out.println("Im the Top");
  }
}

class Triangle extends Shape {
  @Override
  void getShapeName() {
    System.out.println("Triangle");
  }
}

class Rectangle extends Shape {
  @Override
  void getShapeName() {
    System.out.println("Rectangle");
  }
}

Lets test this

public class AbstractTest {
  public static void main(String[] args) {
    Shape shape;
    shape = new Triangle();
    shape.getShapeName();
    shape = new Rectangle();
    shape.getShapeName();
    shape.whoAmI();
  }
}

Output will be

Triangle
Rectangle
Im the Top

In Java, abstraction is achieving through interfaces and abstract classes. We can get 100% summary using an interface.

Class:

Class
Class

A class is a user-defined plan or prototype that creates objects. It represents a set of properties or systems that are common to all objects of one type. In general, class publications can include these components.

Modifier: A class is something general or have default access (see this for details).

Class Name: The name must begin with an initial letter (zoom in by convention).

Superclass (if any): If the name of the parent (superclass) in the class is before the keyword, it is extended. A class can extend (subclass) to only one parent.

Interfaces: If there is a list of interfaces separated by commas activated by the class, the previous password is activated. A class can implement more than one interface.

Body: Class body surrounded by brackets.

Object: 

Object
Object

It is a basic unit of object-oriented programming and represents real-life institutions. A typical Java program creates many objects and, as you know, interacts with appeal methods. An object consists of:

Status: It is representing by the properties of an object. It also reflects the properties of an object.

Behaviour: It is representing by the cruelty of an object. And it reflects the reaction of an object with other objects.

Identity: It gives an object a unique name and it allows one object to interact with other objects.

Method:

A method is a collection of statements that perform a specific function. A system can do a certain job without giving anything back. The method allows us to reuse the code without having to retype the code. In Java, each system must part of a different class than languages ​​such as C, C ++, and Python. Methods are time savers and help us to reuse the code without having to re-type the code.

Method statement

In general, formal publications have six components:

  • Access Modifier: Defines the type of access to the system, from where it can access your application. In Java, there are 4 types of access specifications.
    • General: Access to all classes in your app.
    • Security: Accessible within the defined package and its subclass (S) (including subclasses declared outside the package)
    • Private: Accessible only within the defined class.
    • Default (published/defined without any modifier): can access the same class and its class defined package.
  • Return Type: The data type or value of the value given by the method is invalid if not returned.
  • System Name: The rules for field names also apply to system names, but the convention is slightly different.
  • Parameter list: The list of input parameters separated by commas, before their data type, is defined in closed parentheses. If there are no parameters, you must use empty brackets ().
  • Exceptions List: Gradually you can throw out the desired exceptions; you can specify these exceptions (s).
  • Methodology: It is inserted between the brackets. The code you need to execute to perform the desired operation.

Messaging:

 Objects communicate with each other by sending and receiving information. A message for an object is a request to execute a procedure and therefore a function of the object to receive the desired result. Sending messages is the name of the object, the name of the function, and the information to be sent.

Polymorphism:

 Objects can take more than one form depending on the context. The program will determine what meaning or use is required to execute the object, minimizing the need to duplicate the code.

Advantages of OOP

Advantages of object-oriented programming
Advantages of object-oriented programming

A major advantage of object-oriented programming techniques over procedural programming techniques is that programmers can create modules that do not need to be modified when a new type of object is added. A programmer can create a new object that simply inherits many of its elements from existing objects. This makes it easy to change object-oriented programs.

OOPL – Objective-based languages

Object-oriented programming language (OOPL) is a high-level programming language based on the object-oriented format. One needs an object-oriented programming language to perform object-oriented programming. Most modern programming languages ​​are object-oriented; however, some older programming languages, such as Pascal, offer object-oriented versions. Some examples of object-oriented programming languages ​​include Java, C ++, and Smalltalk.

Simula is crediting with being the first object-oriented programming language, and the most popular OOP languages:

  • Java
  • JavaScript
  • Python
  • C ++
  • Visual Basic .NET
  • Ruby
  • Scale
  • PHP

Conclusion

Thanks for reading the article Object-Oriented Programming as an essential component in System design and architecture.

My articles on medium