polymorphism – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Thu, 14 Mar 2024 16:53:13 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg polymorphism – Programmerbay https://programmerbay.com 32 32 What Are the Five Main Features of OOPs? https://programmerbay.com/what-are-the-five-main-features-of-oops/ https://programmerbay.com/what-are-the-five-main-features-of-oops/#respond Wed, 13 Mar 2024 16:28:49 +0000 https://programmerbay.com/?p=5082 Object-Oriented Programming or OOPs is a programming paradigm that revolves around the concept of object, which contains properties and methods. 
It combines a group of related attributes and behaviour within a single unit named class, that enhances program design structure and code readability.
Further, it also resolves drawbacks of Procedural programming i.e  code complexity, unusable code.
 
Problem with Procedural Programming?
Procedural Programming follows top-down approach, meaning a program is viewed as a series of sequential steps.
In procedural programming, a program is divided into various procedures or functions which operate on data, the issue with this approach is, when the program grows larger, the code redundancy, maintainability, and complexity increases.
 
Both procedural and object-oriented are imperative programming.
 

Feature of OOPs

 
Object-Oriented programming focuses on binding attributes and behavior of a real-world entity represented using an object and supports features like abstraction, encapsulation, inheritance, and polymorphism. These are the following OOPs features.
  • Classes
  • Objects
  • Inheritance
  • Polymorphism
  • Data Abstraction and Encapsulation

Other OOPs Features:

  • Dynamic Binding
  • Message Passing

Here are the five main features of OOPs

Classes:

A class is the user-defined data type and the main building block of object-oriented programming.
It is an identifiable entity that can have some descriptive properties. It is a user-defined data type that holds data members and member functions in a single unit. It is like a blueprint of an object.
 
For example, consider an entity “Laptop” , what attributes, you can think of? RAM, OS, memory, manufacturer name, model name and so on.
 
public class Laptop
{
String modelName;
String manufacturerName;
String ram;
String memory;
String os;


void boot(){


}
}

The above code represents, how a laptop’s attributes and its behavior are put together in a single place.

Objects:

Objects are like a signature of a class. An object is an instance of a class without an object, no memory is allocated to a class’s data members or member function.
With an object of a class, we can access the data members and member functions that can be accessed (as per the private, public, protected accessibility scope).
 
An object represents a real-world entity, having a set of attributes and behavior. However, an object can’t be simply declared as same as primitive types.
 
Continuing Laptop’s example, we can define multiple Laptop objects and each object would get the same attributes and behavior declared in the respective class.
Laptop obj = new Laptop();
Laptop obj1 = new Laptop();
Laptop obj2 = new Laptop();

Initializing fields of a single object .

obj.modelName = " Pavilion 15";
obj.manufacturerName = "HP";
obj.ram = "4 GB";
obj.memory = "1 TB";
obj.os = "Windows";

Inheritance:

The ability to inherit the properties of one class to another, or inherit the properties from a base class to an inherited class is known as the concept of Inheritance.
With the help of inheritance, we can use the data members and member functions of a class to another.
 
  • It enables a class to acquire or get the properties from another class
  • It makes code reusable
  • It makes easier to add new features or methods to a class
  • It provides an overriding feature which allows a child class to have a specific implementation of a method defined in the parent class
 
A class that is inherited by other classes is termed as super class or parent class or base class, whereas a class that extends another class is termed as sub-class or child class
 
Features of Inheritance :
  • Code Reusability
  • Ease to add new feature
  • Overriding
 
Inheritance can be classified into majorly 5 types : Single Inheritance, Multilevel inheritance, Hierarchical Inheritance, Multiple Inheritance and Hybrid Inheritance
 
Syntax :
class Laptop extends Electronics
{


}

We can also control or check the data members and member functions to be accessed in the child classes by the means of access specifiers, types of inheritance, access specifiers, and their respective access will be discussed in later articles.

Polymorphism:

Polymorphism is best defined in one statement i.e., “Polymorphism means one name many forms”.
 
It can be best explained with an example with a comparison to C language; in C language there was one limitation that we can not use the already used function name, but C++ provides us a new feature of Polymorphism, by which we can use the same function name again and again with different signatures.
 
 
The in-depth topic of Polymorphism will be discussed in later articles.

Data Abstraction and Encapsulation:

 
Data Abstraction means hiding the background details and provide only essential details. one of the most common examples regarding this feature is Television, in television we control it with help of a remote and, know its external or the features to be used most whereas we don’t know the internal working of Television.
 
 
Encapsulation means binding up data under a single unit. A class is one of the main implementations of this concept. It can be viewed as a wrapper that restricts or prevents properties and methods from outside access. With the help of access modifiers such as private, protected, and public keywords, one can control the access of data members and member functions

Other OOPs Features:

Dynamic Binding:

Dynamic Binding which is also known as Late binding or run-time binding, is a process of executing the part of the code at runtime.
Sometimes we need to access some part at the runtime if we need then we can use this approach. We use virtual functions to achieve Dynamic Binding.

Message Passing:

In this, objects pass the message to each other in order to contact each other. Like when we want 2 or multiple objects to contact each other it is possible with the OOP.
 

Frequently Asked Questions

 

Q1. What is an object in OOPs?

An object can be viewed as a real-world entity which has attributes and behaviour. For example, a person, it can have attributes like name, age, gender and behaviour such as talking and walking. These properties are put together within a single unit named class. Therefore, an instance of a class is known as an object.

Q2. Which feature of oops is described as the reusability of code?

Inheritance is the feature of OOPs that describes the reusability of code. It provides the ability to inherit attributes and behaviours from one class to another class. A child class can access and use methods and fields of the parent class which leads to code reusability.


Q3. Which feature of oop is illustrated by function overloading?

Polymorphism is the feature of OOPs that is illustrated by function overloading or method overloading. Method overloading signifies a method having the same name can exhibit multiple functionalities based on its parameters.


Q4. Which two features of oops are the same?

Encapsulation and Abstraction are two features of OOPs that are the same. Encapsulation can manage accessibility and hide the attributes and behaviour of an object. In same way,  Abstraction represents only essential data to a user.


Q5. What are the basic principles of OOPs?

Encapsulation, Data Abstraction, Polymorphism and Inheritance are 4 basic principles of Object-Oriented Programming. They are also known as the four pillars of OOPs.

 

]]>
https://programmerbay.com/what-are-the-five-main-features-of-oops/feed/ 0
Difference Between Static And Dynamic Polymorphism https://programmerbay.com/difference-between-static-and-runtime-polymorphism/ https://programmerbay.com/difference-between-static-and-runtime-polymorphism/#respond Wed, 13 Mar 2019 09:37:41 +0000 https://www.programmerbay.com/?p=2592 Polymorphism is one of the essential OOPs feature that can be defined as “ability to take multiple forms”. Programming languages such as Java, C++ use method overloading and method overriding to implement this OOPs feature.

However, it can be classified into Static and Dynamic polymorphism ( Runtime Polymorphism ) . The main difference between them is, one is resolved at compile-time and other resolved at run time.

Difference Between Static And Dynamic Polymorphism ( Runtime Polymorphism )?

BasisStatic PolymorphismRuntime Polymorphism
DefinitionIt can be defined as a process in which a function call with an object resolve at compile time by the compilerIt can be defined as a process in which a function call with an object resolve at runtime
Call ResolutionCall is settled by the compilerCall isn't settled by the compiler
Other NameCompile-time Polymorphism and Early bindingDynamic binding and Late binding
Achieved ByFunction overloading and Operator overloadingPointers and virtual functions
ExecutionIt is analyzed early at compile time so it provides fast executionIt is slow because it is analyzed at the runtime
FlexibilityIt is managed and executed at compile time which makes it less flexibleIt is more flexible because of its execution at runtime

Static Polymorphism

Static polymorphism is additionally termed as compile-time polymorphism, which implies that one can write numerous methods in a program with the same name, performing distinctive tasks.

However, it gives the client or the software engineer efficient and better comprehensibility of code.

Runtime Polymorphism ( Dynamic Polymorphism )

This is otherwise called Dynamic Polymorphism. It is a procedure in which a call to an overridden method is settled at runtime, which is the reason it is termed as runtime polymorphism.

Key differences

  • In Static Polymorphism, the call is settled by the compiler, whereas; In Run time Polymorphism, the call isn’t settled by the compiler.
  • It is otherwise called as Compile-time Polymorphism and Early binding, whereas; it is otherwise called Dynamic binding, Late binding and overriding also.
  • Overloading is compile-time polymorphism where more than one functions share a similar name with various parameters or signature and distinctive return type, whereas; Overriding is run time polymorphism having the same function with the same parameters or mark, however, related in a class and its subclass.
  • Static polymorphism is accomplished by function overloading and operator overloading, whereas; Runtime polymorphism is accomplished by pointers and virtual functions.
  • As in static polymorphism, it is analyzed early at compile time so it provides fast execution, whereas; Runtime polymorphism is slow because it is analyzed at the runtime.
  • In Static polymorphism, all the stuff is managed and executed at compile time which makes it less flexible, whereas; Runtime polymorphism is more flexible because of its execution at runtime.

Note: If you think, anything is wrong with the article, please inform us at inform@programmerbay.com

]]>
https://programmerbay.com/difference-between-static-and-runtime-polymorphism/feed/ 0