Beginning OOP Classes | OOP | Class

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 ,

  1. It does not return anything , not even void. It is just “public class-name”
  2. 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!

Functions In-Depth | function

*FYI : I am using the word “function” here but pls note that “function” and “method” can be used interchangeably.*

Last Of The Function

Alright , this would be the last of the topics on functions I promise. In this post , we would be going into 2 main areas , input parameters and return values. So far , our examples involve no more than printing some basic words or numbers using functions. That is actually not how functions are used in real-world. Lets go back to the example about cooking a dish. We could , if we wish , could come up with many functions for cutting ingredients ,

Cooking With Function

Cut-Vege-Function() { ... }
Cut-Meat-Function() { ... }
Cut-Fruit-Function() { ... }

There is absolutely nothing wrong with the above functions. However , you could see just from these three examples that something is very wrong with our logic. If there are more than 3 types of ingredients to be cut , say 100 types , we would have to be creating over 100 similar functions to cut each type. Instead , we could have created a function called “Cut” and give it the type to cut. Like this ..

Cut(Vege) { ... }
Cut(Meat) { ... }
Cut(Fruit) { ... }

Wait , we have not finished our story. What happened to those objects after being cut? Are you going to eat them? Surely not. You probably would prepare them for further steps. Perhaps you might boil the vegetable , bake the meat and freeze the fruit and so on. So lets go back and change our code with “return” statement.

cut-vege = Cut(Vege) { ... return cut-vege; }
cut-meat = Cut(Meat) { ... return cut-meat; }
cut-fruit = Cut(Fruit) { ... return cut-fruit; }

Of course , you would notice that there are two variables here , one of them is inside the function and the other outside it. I wrote this with the intend of going though variables scope/lifetime one more time. Apparently the variable ‘cut-fruit’ within the function ends when the function also ends.

C# Function Example

Here is a function with parameters and a return ,

public int Sum(int x , int y)
{
    return x + y;
}

So you put in two numbers – internally within the function , they are named x and y – and receive an integer which is the sum of the two numbers entered , as the “int Sum” stated. If we are to put

public void Sum(int x , int y)

It means the function would return void or nothing to whoever called it.

Calling a Function

So how do we call a function? As you might have guessed , it depends on what are the parameters and what does it return , if it returns anything at all. Here is how the above example would be called ,

int sum;
sum = Sum(3,5);
Console.WriteLine("The sum of 3 and 5 is : {0}" , sum); //prints out "The sum of 3 and 5 is : 8

This is a more flexible version of the above line allowing you to definte x and y as any numbers and substituting them when compiling the program.

Console.WriteLine("The sum of {0} and {1} is : {2}" , x , y , sum(x,y));

Hope this has given some help to you and you have enjoyed it as much as I have writing this. Cya! Here is an tutorial on basic variable usage if you have not gone though it.

Programming Functions

Lets talk about a new topic today. Functions aka Methods. We looked at them here and there but never really talked about them in detail until now. Here are some of them functions in SQL that we have looked at ,

  • Count() function – select count(*) from employee; –> returns the number of rows.
  • Password() function – to encrypt the password text

Of course for past few days we have also been looking at functions in C/C++/C#/Java/PHP and so on. So what are functions? And why do we need them at all? TO illustrate why we need functions , here is an example that has nothing to do with computers. Lets cook!

To cook a dish or a meal , first of all we need to prepare ingredients , lets make a list of the steps we will be doing ,

  1. skin the chicken
  2. boil the vegetables
  3. prepare the broth
  4. make the sauce

Each of the above mentioned steps could be thought of as a function by itself. For example , skin the chicken. Here are the steps you might need to skin a chicken.

  1. buy a frozen chicken at the market
  2. thaw the chicken
  3. choose a knife
  4. remove the skin

What this means is that instead of saying all these every time we need to skin a chicken , we just need to say “skin the chicken” and it is understood what are the steps involved. Also when there is a need to change the way the chicken is skinned , we would only need to modify it in one place and one place only. That simplify the process.

Of course , we use functions all the time in our daily life. When we say “I took the bus to here” , what you meant was you waited for the bus , boarded the bus , paid the bus fare and finally alighted from the bus. I think you get the drift by now.

So how do we use it in programming context? Here is one function we have used ,

Console.WriteLine("Hello");

the function , “WriteLine” , is neither written by you nor me , yet it is available to whoever that is writing a C# language. Thats what makes it so special , you would only need to write it once and could be reused not only by you but everyone else , given the right permission.

I hope you are now much clearer about what are functions in the context of programming and also how we use them. Till next time. Cya!