OOP java Class Example
For the example program on Object-Oriented Programming (OOP) , I will be using Java instead of the usual suspects such as C# or PHP. The syntax of the class will be similar and so are most of the terms I would be using in this post. Here is the code from Oracle website itself , to ensure all the necessary steps are correct.
public class Bicycle {
// the Bicycle class has three fields or attributes
public int cadence;
public int gear;
public int speed;
// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
// the Bicycle class has four methods or functions
public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
Lets take a look at it before we modify it our own liking. For now , pls ignore the word “public” , which defines the permission for this class , eg.who can access this. We will be going through “public” , “private” , “protected” in future posts. For now just take it that “public” means its open to all , which could be good or bad thing as we shall see.
So we are talking about a type called “Bicycle” , which has 3 attributes or fields to describe it , cadence , gear and speed. Besides that , it can also do a few functions , such as “SpedUp” and so on. I think by now you should be familiar with functions , or at least be able to read it. If not , pls read my previous post one more time here.
OOP Terms : Class vs Instance
Here I would like to pause for a while to take a brief look at the differences between “Class” vs “Instance” of a class in term of OOP. Bicycle , which we have defined above , is a class , so is a man or a woman. But every bicycles are different in their own ways and similarly each man and woman are also different. You and me are both human , to be more precise “Homo Sapien” , but clearly there are differences enough to tell us apart from one another. So how do we create , or instantiate , a new bicycle?
Syntax For Instantiating An OOP Class
ClassName InstanceName = new ConstructorName();
Examples ,
Bicycle b1 = new Bicycle(); Bicycle b2 = new Bicycle();
Constructor is a special type of function that is designed specifically to create an instance. The keyword is “new” which creates a new instance using the constructor given. But if you look above , the constructor has 3 parameters , which means it is not the default constructor rather it has override it. Think of it like saying “She is a lady , but very find one indeed”. So she has gone beyond what is considered a lady , same for the constructor we have here. So do you tell the difference between a constructor and a normal function? A constructor has these attributes ,
- It does not return anything , not even void. It is just “public class-name”
- The name of the constructor must be same as the name of the class , including the first letter in capital.
To make a new instance of a Bicycle using the constructor given , we would have to change our code ,
Bicycle b1 = new Bicycle(10 , 15 , 6); Bicycle b2 = new Bicycle(15 , 9 , 2);
We now have 2 new bicycles! They are both bicycles but they are not the same. Whatever you do to one doesn’t affect the other in any way. This is one of the main concepts of OOP so pls besure to understand it.
OOP Class Ends!
That is it for now but I shall be going more in-depth into this example and also on OOP theory. Cya!