Object-Oriented Programming
Introduction
OOP is a programming methodology combing code into objects, which are instance of classes. Most of the Object-Oriented Programming (OOP) Languages are class-based, which indicates that a class defines the data attributes and functions and provides guidance for constructing objects, which instance of the class.
Access Specifiers / Modifiers used in oops concepts
Access specifiers will decide the accessibility level of member variables and member functions in a particular class. In OOP there are three main access specifiers.
- Public Access Specifiers:
If a variable or a method is declared under the public access specifier, that variable or method can be used by any of the classes in the same project (namespace).
2. Private Access Specifiers:
If a variable or a method is declared under a private access specifier, that variable or method can only be accessed by the class in which it is declared. This means those items will be limited to the class itself.
3. Protected Access Specifiers:
If a variable or a method is declared under a protected access specifier, that variable or method can be accessed only by specific users.
What is the OOP Concept
There are 4 main structures of OOP concepts that we use in programming. These structures are also referred to as OOP’s building blocks.
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction
- Encapsulation
- Encapsulation is the process of hiding data inside a single class while making the value of the variable accessible through specific methods, providing data abstraction and protection.
- This ensures the modularity and integrity of the data.
The way that Encapsulation programming hides information
- In Encapsulation, data hiding is controlled using Getter and Setter methods.
- This manages access to the private attributes of a class.
Getter:
- The Get method will always be a public method that returns the value of a specific private variable within a class.
- This method is also referred to as the accessor method.
public class Person {
private String name;
public String getName(){
return name;
}
}Setter:
- This is also a public method that is used to set some values or update the value of a specific variable within a class.
- This method is also referred to as the mutator method.
- The Set method does not return any value from the function.
- Most of the time, it is a parameterized method.
public class Person{
private String name;
public void setName(String newName){
name = newName;
}
}Get and Set method Example Code
public class Person{
private String name;
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
}2. Inheritance
- This is a mechanism that makes it possible for one class to obtain the attributes of another.
- Programmers are allowed to create new classes that reuse the data numbers and methods of an existing class.
- In such a situation, the existing class will become the parent or base class.
- The new class will become a child or derived class.
Syntax:
class Animal {
// method and fields
}
class Dog extends Animal {
// methods and fields of Animal
// methods and fields of Dog
}- All the public and protected fields and methods that are contained inside the class will be available to the child class.
- In Java, the “extend” keyword will be used to perform inheritance.
- In C#, we use the colon [:] symbol to perform inheritance.
class Animal {
// method and fields
}
class Dog : Animal {
// methods and fields of Animal
// methods and fields of Dog
}“is — a” relationship in Inheritance
- Inheritance can be identified by using the keyword “is-a”
- We only use inheritance when there is an “is-a” relationship between any two classes.
For example:
— A Dog is an Animal
— The Car is a Vehicle
— An Apple is a Fruit
3. Polymorphism
- This allows objects of different classes to respond to the same method call in different ways.
- We cannot include two or more methods with the same method signature inside a single class.
class student {
public void stdMarks(int y) {
// body
}
public void stdMarks(int x) {
// body
}
}In this example, both given methods contain the same method signature. So you are not able to use it like this.
Get Vinuri Disara’s stories in your inbox
Join Medium for free to get updates from this writer.
Examples for Polymorphism
class student {
public void stdMark() {
// body
}
public void stdMark(int x) {
// body
}
}class student {
public void stdMark(int y, int a) {
// body
}
public void stdMark(int x) {
// body
}
}In OOP, Polymorphism can be used in two different ways.
- Method Overriding
- Method Overloading
1.Method Overriding
- This is also known as Dynamic Polymorphism.
- This can only be used with Inheritance.
- Inside the parent class, it’s possible to declare the method, and inside the child class, the same method signature can be declared by overriding the parent class method.
- Since it uses inheritance, both the parent class method and the child class method are available to the child class.
class Language {
public void displayInfo() {
// body
}
}
class Java extends Language {
@override
public void displayInfo() {
// body
}
}
class Main {
public static void main(String[] args) {
// create an object of Java class
Java J1 = new Java();
J1.displayInfo();
// create an object of Language class
Language L1 = new Language();
L1.displayInfo();
}
}2.Method Overloading
- This is also known as Static Polymorphism.
- In this polymorphism type, you can contain an “n” number of methods with the same method name by differentiating the parameter list.
- In this concept, the same method will perform different operations based on the parameters.
void function() {
// .....
}
void function(int a) {
// .....
}
float function (double a) {
// .....
}
float function (int a, float b) {
// .....
}4. Abstraction
- Abstraction hides the implementation details of a method and shows only the functionality to the user.
- In OOP, abstraction can be achieved by using abstract classes or interfaces.
- The abstract is not an access specifier.
Abstract Class
- An abstract class is a restricted class that cannot be accessed by other classes using objects.
- This should have at least one abstract method.
- An abstract class can also contain non-abstract methods.
- Abstract classes should be accessed using inheritance.
Abstract Method
- Abstract methods do not have implementations.
- In an abstract class, abstract methods are only defined.
- When defining abstract methods accessibility level and return type can be decided.
- The body of the abstract method will be written inside the child class using the method overriding concept.
abstract class Animal {
public abstract void animalSound();
public void sleep()
{
// body
}
}- Since a class is an abstract class, we are not capable of creating a class object.
— Interface in Java —
- This is another way of implementing abstraction.
- This is defined by using the keyword “interface”.
- Inside an interface, we can declare methods, but the method does not have an implementation.
- Which means an interface is a completely abstract class.
Example:
interface InterfaceName {
public void MethodName1();
public void MethodName2();
}- The above-mentioned methods will automatically become abstract methods since they do not contain any implementation.
- To access the method inside interfaces, another class should be added, and that class should implement the interface by using the keyword “implement”.
class className implements InterfaceName
{
public void MethodName1() {
// body
}
public void MethodName2() {
// body
}
}
Why we need OOP Concepts
- OOP encourages code reuse, modularity, and simplified developer cooperation.
- OOP presents an organized method of developing software.
- It optimizes the scalability, maintainability, and quality of the code.
- Effective developer collaboration is rendered accessible by OOP.
Constructors in OOP
- A constructor is a unique function that facilitates instantiating a class object.
- The constructor does not have a return type, and the constructor name should always be a class name.
- This will execute at the point of object creation by using the class name.
There are two types of constructors in OOP.
- Default Constructor
- Parameterized Constructor
1.Default Constructor
- There should be only one default constructor in any given class.
- The default constructor can have an access specifier, and usually, it is public.
class className
{
public className() {
// body
}
}Calling the Default Constructor
When creating a class object from another class, the default constructor will be called
ClassName obj = new ClassName();2.Parameterized Constructor
- There can be an “n” number of parameterized constructors in a single class by differentiating the parameter list.
- Usually, parameterized constructors are defined under the public access specifier.
class ClassName
{
public ClassName(int x) {
// Can perform any operations
}
public ClassName(String y, double x) {
// body
}
}Calling the Parameterized Constructor
When creating the class object, the parameter should be passed according to the get-call parameterized constructor
ClassName obj = new ClassName(Parameter_value);Conclusion
By using OOP concept, we can secure and protect the privacy of the codes and also, we can hide the code using OOP concepts.






