Skip to main content

OOP Concepts

What is OOP??? 👇

OOP stands for Object-Oriented Programming and that concept is a programming paradigm based on the concept of "Objects" that contain data and methods. The main purpose of object-oriented programming is to increase the flexibility and maintainability of programs. Modularity for easier troubleshooting, reuse of code, flexibility, and effective problem solving are advantages of the OOP concept.  

Now, let us consider what an object is...

An object is anything that has state and behavior. For example, a car, a dog, a person, etc. At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object that is an instance of that class.
Consider the car object it has state color, model, brand, weight, and as behaviors we can consider a break, accelerate, gear change.

Classes

A class can be defined as a template/ blueprint describes the behavior/state that the object of its type support. On the other hand, the class is the collection of objects.

Constructor

Java Constructor - JavatpointThe constructor is the main part of the OOP concept. The constructor looks like a method and its name must same the class name and it cannot have any return type as void, int, float. Further, A constructor can not be abstract, static, and final.
An object is created using the new keyword and the default constructor of the class is executed automatically. And also, there are two types of constructors.
  • No-argument constructor 
  • Parameterized constructor
Class MyClass{
     MyClass(){
       //body of constructor
    }
     MyClass(parametares){
        //body of constructor
     }
}

Object-Oriented Programming Features

Object Oriented Programming: A curated set of resourcesThere are main four features in the OOP concept.
  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction

Inheritance

The inheritance can be defined as the process where one class acquires the properties of another. The class which inherits the properties of others is known as subclass(derived class, child class) and the class whose properties are inherited is known as superclass(base class, parent class). The "extends" keyword is used to inherit the properties of a class. 
Inheritance in C++ | Real Life Example of Inheritance in C++ - C++ ...
Type of Inheritance
  1. single inheritance
  2. Multilevel inheritance
  3. Hierarchical inheritance
  4. Multiple inheritance
  5. Hybrid inheritance
mytoshikatrainingcenter – MyToshika Blog

Polymorphism

Polymorphism means, the same thing in different forms. In here, use Inheritance, overriding and upcasting and call the subclass method through a superclass reference.
Consider the real-world example, A person at the same time can have different characteristics. Like a person at the same time is a father or husband or son or an employee. so, the same person posses different behavior in different situations. 
There are two types of polymorphism in java.
  1. Compile-time polymorphism - achieve through the method overloading
  2. Runtime polymorphism - achieve through the method overriding

Encapsulation

Mastering Test Automation by - Vinod Rane: Encapsulation
The encapsulation means the variable of a class will be hidden from other classes. There, Use the "Private" access modifier for hiding variables and Provide public setter and getter methods to modify and view the variable values. 
Consider the real-world example, Encapsulation like a capsule. Basically, capsule encapsulate several combinations of medicine. If a Combination of medicine is variable and method, the capsule acts like a class, and the whole process is called encapsulation.
Why use encapsulation
  • Better control of class attributes and methods
  • Increased security of data
  • Flexible -: The programmer can change one part of the code without affecting other parts.
Abstraction

Abstraction is the process of hiding certain details and showing only essential information to the user. In the abstraction, must be used abstract keyword and the abstract keyword is a non-access modifier.
Consider the real-world example, Diving a car. The man only knows that pressing the accelerator will increase the speed of the car or applying brakes will stop the car. but he does not know about how on pressing accelerator the speed is actually increasing. In other words, he does not know about the inner mechanism of the car.    
Abstract class -: Abstract class is a restricted class that cannot be used to create objects.
Abstract method -: Abstract method can only be used in an abstract class ad it does not have a body.

Comments

Popular posts from this blog

Java Introduction

Welcome to my blog. Here, I think to write a very important article about the java programming language. Java language is a very important popular programming language for computer science students and also that is a platform. Java is used to develop web applications, mobile applications, games, and much more. It is developed by Sun Microsystem in 1995. A Java code is executed by Java Virtual Machine(JVM). JVM  is an abstract computing machine virtual machine interface. It is written by C programming language and it is operating system dependent. Java virtual machine is the main component in java language. we called java is a platform-independent and reason is that, Java virtual machine.  when we compile a java source file(java program) a Byte code is generated. This byte code can be used to run on any platform. Java Features and Benefits Java is an object-oriented language  Platform independent language It is open-source and free  Memory management Huge ...

Drug and Alcohol Prevention

This article is based on the speech and the presentation about “Drug and Alcohol Prevention” was delivered by Mr. Pubudu Sumanasekara, who is executive director of Alcohol and Drug center (ADIC) of Sri Lanka. He made this valuable speech at the UCSC auditorium, under Enhancement program for the 1 st year student of UCSC undergraduate program on 7 th November 2018. As I understood, this lecture was mainly based on Drugs, alcohol and their influence on life. This speech has contained more facts, advice and social issues and science beyond the drugs through his experience. Drug menace is a serious problem among the youth of the world. Unfortunately, so many young generations of the present world have addicted to using drugs. It is comprised young generation of Sri Lanka. This leads to many social problems. The way reason is that they get addicted the drugs because of the parent’s default. Because they don’t attend to children, they refer to a fault. As a result, children campaign ...