Skip to content

Commit 980a238

Browse files
authored
Merge pull request #73 from Yasas4D/main-oop-concepts
Add main OOP concepts
2 parents 6b72837 + 4dde16c commit 980a238

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

OOP Concepts/Abstraction.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Abstraction is a process of hiding implementation details and exposes only the functionality to the user.
2+
// As an example, a driver will only focus on the car functionality (Start/Stop -> Accelerate/ Break)
3+
// He/she does not bather about how the Accelerate/ brake mechanism works internally. This is how the abstraction works.
4+
5+
// Abstract class
6+
public abstract class Car {
7+
public abstract void stop();
8+
}
9+
10+
// Concrete class
11+
public class Ford extends Car {
12+
// Hiding implementation details
13+
@Override
14+
public void stop(){
15+
System.out.println("Ford::Stop");
16+
}
17+
}
18+
19+
public class Main {
20+
public static void main(String args[]){
21+
Car obj = new Ford(); // Car object
22+
obj.stop(); // Call the method
23+
}
24+
}
25+

OOP Concepts/Encapsulation.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//Encapsulation is the process of wrapping code and data together into a single unit.
2+
//Just like a capsule which is mixed of several medicines. The medicines are hidden data to the end user.
3+
4+
// A Java class which is a fully encapsulated class.
5+
public class Car{
6+
7+
// private variable (Could not access directly)
8+
private String name;
9+
10+
// getter method for name
11+
public String getName() {
12+
return name;
13+
}
14+
15+
// setter method for name
16+
public void setName(String name){
17+
this.name = name
18+
}
19+
20+
}
21+
22+
23+
// Java class to test the encapsulated class.
24+
public class Test{
25+
26+
public static void main(String[] args){
27+
28+
// creating instance of the encapsulated class
29+
Car car = new Car();
30+
31+
// setting value in the name member
32+
car.setName("Ford");
33+
34+
// getting value of the name member
35+
System.out.println(car.getName());
36+
}
37+
38+
}

OOP Concepts/Inheritance.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//Inheritance is the process of one class inheriting properties and methods from another class in Java.
2+
//It's just like father and son relationship.
3+
//Son inherit fathers things as well as things of his own
4+
//Here is a Car example senario to that
5+
6+
7+
// super class
8+
class Car {
9+
// the Car class have one field
10+
public String wheelStatus;
11+
public int noOfWheels;
12+
13+
// the Car class has one constructor
14+
public Car(String wheelStatus, int noOfWheels)
15+
{
16+
this.wheelStatus = wheelStatus;
17+
this.noOfWheels = noOfWheels;
18+
}
19+
20+
// the Car class has three methods
21+
public void applyBrake()
22+
{
23+
wheelStatus = "Stop"
24+
}
25+
26+
// toString() method to print info of Car
27+
public String toString()
28+
{
29+
return ("No of wheels in car " + noOfWheels + "\n"
30+
+ "status of the wheels " + wheelStatus);
31+
}
32+
}
33+
34+
// sub class
35+
class Ford extends Car {
36+
37+
// the Ford subclass adds one more field
38+
public Boolean alloyWheel;
39+
40+
// the Ford subclass has one constructor
41+
public Ford(String wheelStatus, int noOfWheels,
42+
Boolean alloyWheel)
43+
{
44+
// invoking super-class(Car) constructor
45+
super(wheelStatus, noOfWheels);
46+
alloyWheel = alloyWheel;
47+
}
48+
49+
// the Ford subclass adds one more method
50+
public void setAlloyWheel(Boolean alloyWheel)
51+
{
52+
alloyWheel = alloyWheel;
53+
}
54+
55+
// overriding toString() method of Car to print more info
56+
@Override
57+
public String toString(){
58+
return (super.toString() + "\nCar alloy wheel "
59+
+ alloyWheel);
60+
}
61+
}
62+
63+
// driver class
64+
public class Main {
65+
public static void main(String args[]){
66+
67+
Ford ford = new Ford(3, 100, 25);
68+
System.out.println(ford.toString());
69+
}
70+
}

OOP Concepts/Polymorphism.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//Polymorphism is the ability to perform many things in many ways.
2+
public class Car{
3+
4+
public void speed() {
5+
}
6+
7+
public void speed(String accelerator) {
8+
}
9+
10+
public int speed(String accelerator, int speedUp) {
11+
return carSpeed;
12+
}
13+
}

0 commit comments

Comments
 (0)