Paradigms of Programming Languages
Programming paradigms are fundamental styles or approaches to programming that help
organize and structure code. Over the years, several paradigms have emerged, each offering
distinct ways to approach problem-solving in software development. Some of the main
programming paradigms include:
1. Imperative Programming: Focuses on how to perform tasks using statements that
change the program's state. Common in languages like C, Java, and Python.
2. Declarative Programming: Emphasizes what should be done rather than how to do it. It
allows the programmer to express the logic of computation without describing its control
flow. Examples include SQL and HTML.
3. Functional Programming: Based on mathematical functions, where computation is
treated as the evaluation of mathematical functions. Examples include Haskell and Lisp.
4. Object-Oriented Programming (OOP): Based on the concept of "objects" which can
contain data in the form of fields (attributes or properties) and code in the form of
methods (functions or procedures). Java, C++, and Python are common OOP languages.
5. Logic Programming: Uses formal logic to express computation. Prolog is a well-known
example.
Basic Concepts of Object-Oriented Approach
Object-Oriented Programming or Java OOPs concept refers to programming languages that
use objects in programming. They use objects as a primary source to implement what is to
happen in the code. Objects are seen by the viewer or user, performing tasks you assign.
Object-Oriented Programming revolves around the following core concepts:
1. Class: A blueprint or template for creating objects. A class defines attributes and methods
that its objects will have.
2. Object: An instance of a class, which encapsulates both data (attributes) and behavior
(methods).
3. Encapsulation: The bundling of data (attributes) and methods (functions) within a class.
Encapsulation hides the internal state and only exposes a controlled interface to the
outside world.
4. Inheritance: A mechanism where a new class (child) can inherit attributes and methods
from an existing class (parent). This allows for code reuse and hierarchical relationships
between classes.
5. Polymorphism: The ability for different classes to provide a common interface for a
method, meaning that the method can behave differently depending on the object
invoking it.
6. Abstraction: Hiding the complex implementation details and showing only the essential
features. It simplifies the interaction with objects and allows users to work with high-
level functionality.
7. Message Passing: Objects communicate by sending and receiving messages, usually in
the form of method calls. This allows objects to collaborate to solve problems.
Comparison of Object-Oriented and Procedure-Oriented Approaches
Object-Oriented Programming Procedure-Oriented Programming
Aspect
(OOP) (POP)
Focuses on functions or procedures
Focus Focuses on objects and data.
that perform actions.
Program is organized around classes Program is structured around
Structure
and objects. functions or procedures.
Data Handling Data is encapsulated within objects. Data is separate from functions.
High reusability due to inheritance Reusability is achieved through
Reusability
and polymorphism. function calls.
Easier to maintain due to Harder to maintain due to lack of data
Maintainability
encapsulation and modularity. encapsulation.
Supports modularity with objects and Achieves modularity through
Modularity
classes. functions.
Complexity Better suited for handling large, Works well for simpler, smaller
Handling complex systems. applications.
Benefits of Object-Oriented Programming (OOP)
1. Modularity: OOP encourages modular design, where objects can be developed and
tested independently.
2. Reusability: Code can be reused through inheritance and composition, reducing
redundancy.
3. Maintainability: Changes made in one part of the code (class) will not affect other parts,
improving maintainability.
4. Scalability: OOP makes it easier to scale applications due to its modular and hierarchical
nature.
5. Encapsulation: Internal implementation details are hidden from the outside, reducing
complexity and increasing security.
6. Polymorphism: Enables flexibility by allowing objects to take many forms, making code
more extensible.
7. Improved Productivity: With OOP, developers can build applications more quickly and
with fewer errors due to its organized and reusable structure.
Introduction to Common Object-Oriented Languages
1. Java: A class-based, object-oriented language known for its platform independence
("write once, run anywhere") and wide usage in enterprise and mobile applications.
2. C++: An extension of C that supports object-oriented features like classes and
inheritance. It's widely used in system software, game development, and applications that
require high-performance.
3. Python: A versatile, high-level language that supports multiple programming paradigms,
including OOP. It’s known for its simplicity and readability.
4. C#: A language developed by Microsoft, primarily used for Windows applications, web
services, and game development using the .NET framework.
5. Ruby: A dynamically-typed language focused on simplicity and productivity, with a
strong emphasis on object-oriented principles.
Applications of Object-Oriented Programming (OOP)
1. Software Development: OOP is widely used in creating complex software applications
due to its ability to handle large systems and promote code reuse.
2. Game Development: Many game engines, like Unity, use OOP to manage complex
game logic, character states, and interactions.
3. Web Development: Frameworks like Django (Python), Ruby on Rails, and ASP.NET are
based on OOP principles to manage web application structure and logic.
4. GUI Applications: OOP helps in developing user interfaces by treating interface
elements as objects (e.g., buttons, windows).
5. Simulation Systems: OOP is used in simulations where various entities (objects) interact
with each other, such as in traffic simulation or game AI.
6. Embedded Systems: Although less common, OOP is also used in embedded systems for
complex control systems, such as those in robotics or automotive systems.
7. Database Management Systems: OOP is used in object-oriented databases, where data
is modeled as objects rather than rows and columns.
In Java, Object-Oriented Programming (OOP) principles such as Classes and Objects,
Abstraction and Encapsulation, Inheritance, Method Overriding, and Polymorphism are
implemented using specific syntax and conventions. Let us discuss about the various OOPS
concepts.
1. Classes and Objects in Java
Class
A class in Java is a blueprint or template from which objects are created. It defines attributes
(fields) and behaviors (methods) that the objects will have. Using classes, you can create
multiple objects with the same behavior instead of writing their code multiple times. This
includes classes for objects occurring more than once in your code.
Syntax for a Class:
class Car {
String make;
String model;
// Constructor
public Car(String make, String model) {
this.make = make;
this.model = model;
}
// Method
public void displayInfo() {
System.out.println("Car make: " + make + ", Model: " + model);
}
}
Object
An object is an instance of a class. You create an object by calling the class's constructor.
Objects are the basic unit of OOPS representing real-life entities. They are invoked with the help
of methods. These methods are declared within a class. Usually, a new keyword is used to create
an object of a class in Java.
Creating an Object:
public class Main {
public static void main(String[] args) {
Car car1 = new Car("Toyota", "Corolla"); // Object creation
car1.displayInfo(); // Calling the object's method
}
}
2. Abstraction and Encapsulation in Java
Abstraction
Abstraction is the concept of hiding implementation details and showing only the essential
features. Data Abstraction may also be defined as the process of identifying only the required
characteristics of an object, ignoring the irrelevant details.
For example, during an ATM operation, we only answer a series of questions to process the
transaction without any knowledge about what happens in the background between the bank and
the ATM. In Java, Abstraction is achieved using abstract classes and interfaces.
Abstract Class: A class that cannot be instantiated and may contain abstract methods
(methods without a body).
o Syntax for Abstract Class:
abstract class Shape {
// Abstract method (no implementation)
public abstract double area();
// Regular method
public void displayInfo() {
System.out.println("This is a shape");
}
}
class Circle extends Shape {
double radius;
public Circle(double radius) {
this.radius = radius;
}
// Implementing abstract method
public double area() {
return Math.PI * radius * radius;
}
}
Using the Abstract Class:
public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5);
System.out.println("Area of Circle: " + circle.area()); //
Polymorphic behavior
circle.displayInfo(); // Regular method from abstract class
}
}
Encapsulation
Encapsulation involves bundling the data (attributes) and methods that operate on the data
within a class, and restricting direct access to some of the object’s components. Encapsulation
can be achieved by declaring all the variables in a class as private and writing public methods in
the class to set and get the values of the variables.
It is defined as the wrapping up of data under a single unit. It is the mechanism that binds
together the code and the data it manipulates. In encapsulation, the data in a class is hidden from
other classes, which is similar to what data-hiding does. So, the terms “encapsulation” and “data-
hiding” are used interchangeably.
Syntax for Encapsulation:
class Person {
private String name; // private variable
// Getter method
public String getName() {
return name;
}
// Setter method
public void setName(String name) {
this.name = name;
}
}
Using Encapsulation:
public class Main {
public static void main(String[] args) {
Person p = new Person();
p.setName("John"); // Using setter
System.out.println("Person's name: " + p.getName()); // Using
getter
}
}
3. Inheritance in Java
Inheritance is an important pillar of OOP (Object Oriented Programming). It is the mechanism in
Java by which one class is allowed to inherit the features (fields and methods) of another class.
We are achieving inheritance by using extends keyword. Inheritance is also known as “is-a”
relationship. Inheritance allows a new class (child class) to inherit the properties and methods of
an existing class (parent class). This supports code reusability.
Syntax for Inheritance:
// Parent class
class Animal {
public void makeSound() {
System.out.println("Some sound");
}
}
// Child class inheriting from Animal
class Dog extends Animal {
public void makeSound() {
System.out.println("Woof");
}
}
Using Inheritance:
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // Outputs: Woof
}
}
In the example above, Dog inherits the makeSound method from Animal and overrides it to
provide its own implementation.
4. Polymorphism in Java
The word ‘polymorphism’ means ‘having many forms’. In Java, polymorphism refers to the
ability of a message to be displayed in more than one form. This concept is a key feature
of Object-Oriented Programming and it allows objects to behave differently based on their
specific class type.
Polymorphism is the ability of an object to take many forms. It allows you to use a parent class
reference to refer to child class objects and invoke the appropriate method.
Real-life Illustration of Polymorphism in Java: A person can have different characteristics at
the same time. Like a man at the same time is a father, a husband, and an employee. So the same
person possesses different behaviors in different situations. This is called polymorphism.
There are two types of polymorphism in Java:
Compile-time polymorphism (Method Overloading): Compile-Time Polymorphism in
Java is also known as static polymorphism. It is achieved by having multiple methods
with the same name but different parameters.
Runtime polymorphism (Method Overriding): Runtime Polymorphism in Java known
as Dynamic Method Dispatch. It is achieved when a subclass overrides a method of its
parent class.
Compile-time Polymorphism (Method Overloading):
Method overloading occurs when two or more methods in the same class have the same name
but differ in the number or type of parameters. Functions can be overloaded by changes in the
number of arguments or/and a change in the type of arguments.
Syntax for Method Overloading:
class Calculator {
// Overloaded method to add two integers
public int add(int a, int b) {
return a + b;
}
// Overloaded method to add three integers
public int add(int a, int b, int c) {
return a + b + c;
}
}
Using Method Overloading:
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(10, 20)); // Outputs: 30
System.out.println(calc.add(10, 20, 30)); // Outputs: 60
}
}
Runtime Polymorphism (Method Overriding):
This occurs when a subclass provides its own specific implementation of a method defined in its
superclass. It is a process in which a function call to the overridden method is resolved at
Runtime. This type of polymorphism is achieved by Method Overriding. Method overriding, on
the other hand, occurs when a derived class has a definition for one of the member functions of
the base class. That base function is said to be overridden.
Example of Runtime Polymorphism:
class Animal {
public void makeSound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Woof");
}
}
class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog();
Animal myCat = new Cat();
myAnimal.makeSound(); // Outputs: Some sound
myDog.makeSound(); // Outputs: Woof
myCat.makeSound(); // Outputs: Meow
}
}
In the example above, we use an Animal reference to call the makeSound method, but the actual
method executed depends on the object's runtime type (Dog, Cat, or Animal), demonstrating
runtime polymorphism.