Skip to content

Instantly share code, notes, and snippets.

@jattoabdul
Last active March 21, 2018 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jattoabdul/c0062df57208d56db376c8bae066ec78 to your computer and use it in GitHub Desktop.
Save jattoabdul/c0062df57208d56db376c8bae066ec78 to your computer and use it in GitHub Desktop.
A simple implementation of OOP concepts in JAVA
class Calculation {
private int batteryLife = 10;
int z;
int w;
public void addition(int x, int y) {
z = x + y;
System.out.println("The sum of the given numbers:"+z);
}
public void subtraction(int x, int y) {
z = x - y;
System.out.println("The difference between the given numbers:"+z);
}
public void subtraction(int x, int y, int z) {
w = x - y + z;
System.out.println("The addition of the difference between the first two numbers and third numbers:"+w);
}
public void displayResult(String message) {
System.out.println("The Default Result of Calculation is:"+message);
}
public void setBatteryLife (int life) {
batteryLife = life;
}
public double getBatteryLife () {
return batteryLife;
}
}
class My_Calculation extends Calculation {
public void multiplication(int x, int y) {
z = x * y;
System.out.println("The product of the given numbers:"+z);
}
public void displayResult(String message) {
System.out.println("The Dynamically Polymorphosed Result of Calculation is:"+message);
}
}
class Main {
public static void main(String[] args) {
int a = 20, b = 10, c = 15;
String d = "Some Dynamic Values", e = "Few Default Values";
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.subtraction(a, b);
demo.subtraction(a, b, c);
demo.multiplication(a, b);
demo.setBatteryLife(50);
System.out.println("Battery Life is: "+(int)demo.getBatteryLife());
demo.displayResult(d);
Calculation baseClass = new Calculation();
baseClass.displayResult(e);
baseClass.setBatteryLife(60);
System.out.println("Battery Life for the baseClass is: "+(int)baseClass.getBatteryLife());
}
}
// Compile By: javac Main.java
// Run By: java Main
// OUTPUT:
// The sum of the given numbers:30
// The difference between the given numbers:10
// The addition of the difference between the first two numbers and third numbers:25
// The product of the given numbers:200
// Battery Life is: 50
// The Dynamically Polymorphosed Result of Calculation is:Some Dynamic Values
// The Default Result of Calculation is:Few Default Values
// Battery Life for the baseClass is: 60
@jattoabdul
Copy link
Author

The working code can be seen on this REPL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment