What is Object Oriented Programming for Beginners.

object oriented programming system.

Objects and Classes

Objects are the basis of object-oriented programming because, in object-oriented programming, objects communicate with another Object to do any work. Some of the Real-world objects are Phone, Book, Television, Cat, etc. All object has a state in which they are currently in and related behavior according to those states. In OO as well the concept of object is similar. An object has several states and related behavior. Object store its states in fields or instance variable, and behavior is declared using methods or procedures.

Object in a heap inside computer memory

Inheritance

OO objects sometimes share similar properties with each other. PondBird and MountainBird classes defined in the below code pallet have a similar way of eating but they also have different methods that make them unique in themselves. inheritance is a way to decouple this common logic from objects and make it reusable so that code duplication can be avoided. please look at the following diagram to get a clear picture.

Inheritance example
// Base or Parent Class
class Bird {
// All common things for Bird will go here.
public void eat() {
System.out.println("eat food");
}
}
// Derived or Child Classes
class PondBird extends Bird {
// All specific things to Pound Bird goes here.
public void swim() {
System.out.println("swims in pond");
}
}
class MountainBird extends Bird {
// All specific things to Mountain Bird goes here.
public void fly() {
System.out.println("fly in sky");
}
}

Polymorphism

Polymorphism is a biological principle which states that an organism or species can have multiple forms or stages. The same principle can be applied to OOP as well. The Subclasses of a class can define their unique traits and still share the same functionality as a parent class.

Polymorphism example
// Base or Parent Class
class Bird {
// All common things for Bird will go here.
public void eat() {
System.out.println("eat food");
}
public void color() {
System.out.println("yellow");
}
}
// Derived or Child Classes
class PondBird extends Bird {
// All specific things to Pound Bird goes here.
public void swim() {
System.out.println("swims in pond");
}
}
class MountainBird extends Bird {
// All specific things to Mountain Bird goes here.
public void fly() {
System.out.println("fly in sky");
}
@Override
public void color() {
System.out.println("Gray");
}
}
/****************************************************************/class Main {
public static void main(String[] args) {
Bird bird1, bird2;
bird1 = new PondBird();
bird2 = new MountainBird();
bird1.color();
bird2.color();
}
}
#java Main
yellow
Gray

Abstraction

Many objects hide their low-level details from the user so, A user could focus on their task instead of worrying about those details, forex. The airplane pilot may not know all the mechanical parts of the airplane but he can still fly it because of the controllers given to him, This hiding of details is called Abstraction.

interface AirplaneInterface {
public void takeOff();
public void makeAnnouncement();
public void activateAutopilot();
public void landOnAirport();
}
class ConcreteAirplane implements AirplaneInterface {
public void takeOff() {
// complex logic
}
public void makeAnnouncement() {
// complex logic
}
public void activateAutopilot() {
// complex logic
}
public void landOnAirport() {
// complex logic
}
/* hidden from user */
public void engineOptimization() {
// complex logic
}
public void connectToAirportCommunicationChannel() {
// complex logic
}
...
}
class AirplaneTestDrive {
public static void main(String[] args) {
AirplaneInterface plane;
plane = new ConcreteAirplane();
plane.takeOff();
}
}

Encapsulation

Encapsulation is also known as data hiding, where we declare our instance variables as private and only give access to them via methods. Data encapsulation play’s a big role in the data validation part. Forex. If the velocity variable suddenly starts accepting a negative number because It is not encapsulated properly, then that would be a big problem for airplane monitoring. Encapsulation also combines related data and methods so that data and its related methods stay together as a logical unit.

class AirplaneMonitoring {
private double velocity = 0;
public void setVelocity(double velocity) {
/* All validation goes here*/
if(velocity < 0) {
return;
}
// after validating assign the value
this.velocity = velocity;
}
public double getVelocity() {
return velocity;
}
}
class AirplainMonitoringTestDrive {
public static void main(String[] args) {
AirplaneMonitoring airplaneMonitoring = new AirplaneMonitoring();

airplaneMonitoring.setVelocity(-1);
System.out.println(airplaneMonitoring.getVelocity()); // 0.0
airplaneMonitoring.setVelocity(2.25);
System.out.println(airplaneMonitoring.getVelocity()); // 2.25
}

}

Conclusion

OOP helps us model real-world object into a programming construct, which in turn allow us to make a modular application which is extendable, reusable, and non-rigid in nature. In this blog, we have gone through basic building blocks that make any OO language a true OO language. In the end, this is a very vast topic so please consider this text as a tip of an iceberg and if you want to dwell deep in any OO language it is worth spending your time on this paradigm.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store