From 4dde16c31025ff6127a100f9e2d22fc22412154a Mon Sep 17 00:00:00 2001 From: Yasas Sandeepa Date: Sun, 24 Oct 2021 12:39:37 +0530 Subject: [PATCH] Add main OOP concepts --- OOP Concepts/Abstraction.java | 25 ++++++++++++ OOP Concepts/Encapsulation.java | 38 ++++++++++++++++++ OOP Concepts/Inheritance.java | 70 +++++++++++++++++++++++++++++++++ OOP Concepts/Polymorphism.java | 13 ++++++ 4 files changed, 146 insertions(+) create mode 100644 OOP Concepts/Abstraction.java create mode 100644 OOP Concepts/Encapsulation.java create mode 100644 OOP Concepts/Inheritance.java create mode 100644 OOP Concepts/Polymorphism.java diff --git a/OOP Concepts/Abstraction.java b/OOP Concepts/Abstraction.java new file mode 100644 index 0000000..54efae2 --- /dev/null +++ b/OOP Concepts/Abstraction.java @@ -0,0 +1,25 @@ +// Abstraction is a process of hiding implementation details and exposes only the functionality to the user. +// As an example, a driver will only focus on the car functionality (Start/Stop -> Accelerate/ Break) +// He/she does not bather about how the Accelerate/ brake mechanism works internally. This is how the abstraction works. + +// Abstract class +public abstract class Car { + public abstract void stop(); +} + +// Concrete class +public class Ford extends Car { + // Hiding implementation details + @Override + public void stop(){ + System.out.println("Ford::Stop"); + } +} + +public class Main { + public static void main(String args[]){ + Car obj = new Ford(); // Car object + obj.stop(); // Call the method + } +} + diff --git a/OOP Concepts/Encapsulation.java b/OOP Concepts/Encapsulation.java new file mode 100644 index 0000000..88a307f --- /dev/null +++ b/OOP Concepts/Encapsulation.java @@ -0,0 +1,38 @@ +//Encapsulation is the process of wrapping code and data together into a single unit. +//Just like a capsule which is mixed of several medicines. The medicines are hidden data to the end user. + +// A Java class which is a fully encapsulated class. +public class Car{ + + // private variable (Could not access directly) + private String name; + + // getter method for name + public String getName() { + return name; + } + + // setter method for name + public void setName(String name){ + this.name = name + } + +} + + +// Java class to test the encapsulated class. +public class Test{ + + public static void main(String[] args){ + + // creating instance of the encapsulated class + Car car = new Car(); + + // setting value in the name member + car.setName("Ford"); + + // getting value of the name member + System.out.println(car.getName()); + } + +} diff --git a/OOP Concepts/Inheritance.java b/OOP Concepts/Inheritance.java new file mode 100644 index 0000000..b91717d --- /dev/null +++ b/OOP Concepts/Inheritance.java @@ -0,0 +1,70 @@ +//Inheritance is the process of one class inheriting properties and methods from another class in Java. +//It's just like father and son relationship. +//Son inherit fathers things as well as things of his own +//Here is a Car example senario to that + + +// super class +class Car { + // the Car class have one field + public String wheelStatus; + public int noOfWheels; + + // the Car class has one constructor + public Car(String wheelStatus, int noOfWheels) + { + this.wheelStatus = wheelStatus; + this.noOfWheels = noOfWheels; + } + + // the Car class has three methods + public void applyBrake() + { + wheelStatus = "Stop" + } + + // toString() method to print info of Car + public String toString() + { + return ("No of wheels in car " + noOfWheels + "\n" + + "status of the wheels " + wheelStatus); + } +} + +// sub class +class Ford extends Car { + + // the Ford subclass adds one more field + public Boolean alloyWheel; + + // the Ford subclass has one constructor + public Ford(String wheelStatus, int noOfWheels, + Boolean alloyWheel) + { + // invoking super-class(Car) constructor + super(wheelStatus, noOfWheels); + alloyWheel = alloyWheel; + } + + // the Ford subclass adds one more method + public void setAlloyWheel(Boolean alloyWheel) + { + alloyWheel = alloyWheel; + } + + // overriding toString() method of Car to print more info + @Override + public String toString(){ + return (super.toString() + "\nCar alloy wheel " + + alloyWheel); + } +} + +// driver class +public class Main { + public static void main(String args[]){ + + Ford ford = new Ford(3, 100, 25); + System.out.println(ford.toString()); + } +} diff --git a/OOP Concepts/Polymorphism.java b/OOP Concepts/Polymorphism.java new file mode 100644 index 0000000..bf9b8e0 --- /dev/null +++ b/OOP Concepts/Polymorphism.java @@ -0,0 +1,13 @@ +//Polymorphism is the ability to perform many things in many ways. +public class Car{ + + public void speed() { + } + + public void speed(String accelerator) { + } + + public int speed(String accelerator, int speedUp) { + return carSpeed; + } +}