Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions OOP Concepts/Abstraction.java
Original file line number Diff line number Diff line change
@@ -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
}
}

38 changes: 38 additions & 0 deletions OOP Concepts/Encapsulation.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
70 changes: 70 additions & 0 deletions OOP Concepts/Inheritance.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
13 changes: 13 additions & 0 deletions OOP Concepts/Polymorphism.java
Original file line number Diff line number Diff line change
@@ -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;
}
}