What is Object Oriented Programming for Beginners.

object oriented programming system.

Objects and Classes

Object in a heap inside computer memory

Inheritance

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 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

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

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

--

--

--

Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What Is API And Why Should We Use It?

Ansible — Playbooks

Sluggish Spring Boot Tests Riddle

IoT Building Automation Service & Providers

Don’t cache (out of process) part two

Just mail it in

Working with Databases in Laravel

Getting started with Flutter

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
Akash Jain

Akash Jain

Software Engineer

More from Medium

Java #4 | OOP Object Oriented Programming (1)

7 Reasons Java is Perfect for Enterprise Software

How to create a LinkedList class

A screenshot of the initialized LinkedList class

Object Oriented Programming in Java