A reminder of OOP methodology in Java.

Photo by Caspar Camille Rubin on Unsplash

The methodology of “Object Orientated programming” OOP is instead of functions and procedures, the Objects are organized into Classes, classes are part of most programming languages. The components that implement the code. Java, C & C++, and PHP are OOP languages. Think about it; everything can break into an Object. The object can be broken down into components (classes & functions). In java, it can be broken into classes and instances. With the use of Objects, people can create and manipulate the state and behaviour. For example, the source code in describing a bicycle may include a class that defines the back breaks structure, which allows us to add what brand the breaks are and how old. They can make a different class to say it is abilities, brand, chain and chain.

Inheritance method is widely used in java applications. Inheriting create a hierarchy between classes from other classes. It allows new classes to share properties from existing classes — for example, a parent class of bicycle and a subclass of Road Bike. Here the road bike acquires the properties of a bicycle.

public class Bicycle {
//Parent class
}
public RoadBike extends Bicycle {
//Child class
}

Code reuse is the most fundamental part of inheritance because the superclass is passed down to the variables and superclass methods.

Programmers don’t want to people to access their code and change the implementation of it. You want to hide any functionality of your code. One of the method’s to use is encapsulation. Using encapsulation, the programmer hides any attributes such as methods and data within itself. This is done by implementing a getter and setter in code with the access modify set to private. Private attributes are only accessible to class methods within itself.

class bicycle {
private int LockKey;
private String bikeName;
private int age;

//Getter and Setter methods
public int getLock(){
return LockKey;
}

public String getBikeName(){
return bikeName;
}

public int getAge(){
return age;
}

public void setAge(int newValue){
age = newValue;
}

public void setBikeName(String newValue){
bikeName = newValue;
}

public void setLockKey(int newValue){
LockKey = newValue;
}
}
public class Main {
public static void main(String args[]){
bicycle obj = new bicycle();
obj.setLockKey(112233);
obj.setBikeName("scott addict 20");
obj.setAge(2);
System.out.println("Bike Name: " + obj.getBikeName());
System.out.println("Bike Lock Code: " + obj.getLock());
System.out.println("Bike Age: " + obj.getAge());
}
}

Polymorphism

Polymorphism means “many forms”. So using the two methods from above, programmers will be able to write code that exists multiple times in java. Using polymorphism, coders can have two of the same methods/variable or functions, and when the compiler reads the code, it can get two different outputs. Code will have a superclass and a subclass with the same method, but the compiler will get a different answer based on its acting upon code.

It’s easier to show you I suppose.

You main higher level object.


public class Bicycle {
public void type() {
System.out.println("I am a basic bike");
}

}
public class RoadBike extends Bicycle{
@Override
public void type() {
System.out.println("I am a road bike");
}

public static void main(String[] args) {
Bicycle basic = new Bicycle();
basic.type();
RoadBike road = new RoadBike();
road.type();
}

}
//Output
basic.type outputs - I am a basic bike
road.type outputs - I am a road bike

Polymorphism occurs when Overriding and method overloading is implemented.

Interface.

Interface are abstract code; they only supply the actual method names and the parameters. The reason being for using interfaces is that coders can use several different classes by having them all implement from the same class. It can standardise programmers code by making it clearer to read, only defining actual methods that form the interface.

public interface Bicycle {
public int speed(int speed);


}
public class MyBike implements Bicycle {
private String name;

public MyBike(String name) {
this.name = name;
}

@Override
public int speed(int speed) {
if (speed > 0) {
System.out.println(name + " We have some speed");
return 1;
} else {
System.out.println( name + " Currently no speed");
return -1;
}
}
}
public class RobsBike implements Bicycle{
private String name;
private boolean broken;
public RobsBike(String name) {
this.name = name;
this.broken = true;
}



@Override
public int speed(int speed) {
if(true == true) {
System.out.println(name + " is broken");
}
return -1;
}


}
public class Main {
public static void main(String[] args) {
Bicycle johnsBike;
johnsBike = new MyBike("scott addict");
johnsBike.speed(3);

Bicycle robsBike;
robsBike = new RobsBike("Scott speedster gravel");
robsBike.speed(55);

}

}

Interfaces are used to make classes independent.

Programmers using java interfaces are used to make the interface independent from the class implementing the interface. They are giving the programmer more flexibility.

Writing about life.

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