Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design applications and
software. These objects are instances of classes, which are essentially user-defined data types. By using objects, we
can write code that is modular, reusable, and easy to understand.
There are four main concepts in OOP: encapsulation, inheritance, polymorphism, and abstraction.
Encapsulation is the practice of keeping fields within a class private, then providing access to them via public methods.
It’s a protective barrier that keeps the data and code safe from external interference and misuse.
Here's an example in Python:
class Circle:
def __init__(self, radius):
self.__radius = radius
def get_radius(self):
return self.__radius
def set_radius(self, radius):
self.__radius = radius
In this example, __radius is a private attribute of the Circle class. By convention, we use double underscores to denote
private attributes and methods. The get_radius and set_radius methods are used to access and modify the private
__radius attribute.
Inheritance is the process by which one class can acquire the properties (methods and fields) of another. With
inheritance, we can create a general class first then later extend it to more specialized classes.
Here's an example in Python:
class Animal:
def __init__(self, name):
self.name = name
def make_sound(self):
pass
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
def make_sound(self):
return "Woof!"
In this example, Dog is a subclass of Animal and inherits its __init__ method. The Dog class also has its own
make_sound method that returns "Woof!".
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs
when a parent class reference is used to refer to a child class object.
Here's an example in Python:
def make_sound(animal):
animal.make_sound()
my_dog = Dog("Fido", "Golden Retriever")
make_sound(my_dog) # Output: Woof!
In this example, the make_sound function takes an animal argument and calls its make_sound method. By passing a
Dog object to the make_sound function, the make_sound method of the Dog class is called, resulting in "Woof!" being
, output.
Abstraction is a process of hiding the implementation details and showing only the functionality to the user. Another way,
it shows only important things to the user and hides the internal details, for example, sending SMS, you just type the text
and send the message. You don't know the internal processing about the message delivery.
Here's an example in Python:
class BankAccount:
def __init__(self, balance=0):
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount > self.balance:
print("Insufficient funds")
else:
self.balance -= amount
In this example, the BankAccount class has three methods: __init__, deposit, and withdraw. These methods handle the
implementation details of a bank account. To the user, however, they only need to know how to deposit and withdraw
money from the account, not how the account keeps track of the balance.
In conclusion,
Object-Oriented Programming is an powerful programming paradigm useful for building large, complex applications.
Understanding the four main concepts in OOP— encapsulation, inheritance, polymorphism, and abstraction— will help
you write code that is modular, reusable, and easy to understand.
Java Programming Basics
Java is a popular, high-level programming language known for its simplicity, versatility, and object-oriented programming
paradigm.
In Java, everything is a class, which is a blueprint for creating objects. Here's an example of what a simple class might
look like:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
This HelloWorld class has a single method, main, which takes in an array of strings as an argument. The main method
is the starting point for any Java program and is where you'll write the majority of your code.
Inside the main method, we use the System.out.println() function to print the string "Hello, world!" to the console. This is
a common way to test that your Java environment is set up correctly and that you're able to write and run basic Java
programs.
Java also has primitive data types, which are basic types of data that can be used to represent numbers, characters,
and Boolean values. Here are some examples:
int: an integer, such as -5, 0, or 123
float: a floating-point number, such as 3.14 or 12.0