Java ProgrammingIntroduction to OOP in Java Programming Language

Introduction to OOP in Java Programming Language

 

In this article, we are going to start discussing object oriented programming in Java. An important foundational topic that you should understand. So, bear with us.

Classes
Perhaps before delving into classes and objects, it’s better to address an important question: why object oriented programming? Ok, in the past articles, you have learned how to make simple applications that utilized the main function, may be in addition to another function to encapsulate code. That’s fine for small applications, but when your code gets longer and you find yourself having to write reusable code for, say, database connection, user authentication, and log file management, you will admire object oriented programming paradigm. Now that’s enough, let’s see what classes are.

A class is a container for code. No, not like a function. A class may contain properties (variables), and methods (functions), all in one entity. Have a look at the following class:

public class Person {
    
}

Ignore the keyword public for the time being. A class starts with the keyword class, followed by its name, then a pair of curly braces, within which the class members exist. It’s a recommended practice to capitalize the first letter of the class name. If the name is made up of multiple words, each letter of them should be capitalized as well. This is called camel casing. For example, FirstPerson.

You put any name to the file containing a class code, as long as it ends with .java extension. However, if the class is public (a public class can be accessed from outside its package. More details about this in a later article), the class name must match the file name. So to avoid headaches, make sure you name your files with the names of the respective classes they hold.

It is possible to declare multiple classes in the same file. But in this case, only one class can be declared as public. Accordingly, the file name will hold the name of this public class. Have a look at the following:

public class FirstPerson {
    
}

class HighSchoolStudent{
    
}

class JavaProgrammer {
    
}

The compiler will not complain that you added three classes in the same code file. But it will do if you named this file anything other than FirstPerson.java, or if you tried to declare HighSchoolStudent, or JavaProgrammer as public.

Java Programming Course for Beginner From Scratch

Objects
A class is said to be a blueprint for an object. Imagine that you are an engineer, and you are drawing the plan for building a house. A plan in itself is not the house, and it cannot be used as one. However, you can use the same plan to build multiple houses, all sharing the same schematics. That’s the same with classes and objects. The class is the plan. To use it, you have to instantiate an object from this class. This is done by using the keyword new. Consider the following example:

[File: FirstPerson.java]
package ClassExamples;

public class FirstPerson {
    
}
[File: MainClass.java]
package MainClass;

import ClassExamples.FirstPerson;


public class MainClass {

    public static void main(String[] args) {
        FirstPerson ahmad = new FirstPerson();
    }
}

In the first file, we created a class FirstPerson. In the second file, we added our main function, and instantiated an object (ahmad) from FirstPerson class using the keyword new. Notice the brackets that follow the class name. Brackets are normally added when calling a function by its name right? Yes, and classes are not exception. Each class has a constructor function. It is responsible for executing any code that needs to be run at the start of the instantiation process. You normally use it to initialize your variables, or obtain input parameters that will be used later in the class. A constructor function is a special one. It always bears the class name, and it does not return anything, yet it does not use the void keyword. Observe the following example:

public class FirstPerson {
    public FirstPerson(){
        System.out.println("I am the construction function");
    }
}

The construction function prints a message to the screen whenever an object of that class gets instantiated. To demonstrate this, create another object of the same class:

public static void main(String[] args) {
        FirstPerson ahmad = new FirstPerson();
        FirstPerson mostafa = new FirstPerson();
    }

Having run this code, you will see the message “I am a construction function” printed twice on the screen.

More often than not, you will need your constructor function to accept parameters. You pass parameters to the constructor function the same way you do it with any other function. Take a look at the following:

[File: FirstPerson.java]
public class FirstPerson {
    public FirstPerson(String _name){
        System.out.println("My name is " + _name);
    }
}
[File: MainClass.java]
public static void main(String[] args) {
        FirstPerson ahmad = new FirstPerson("Ahmad");
    }

The output of this code will be “My name is Ahmed”.

Conclusion
In this article, we have talked about object oriented programming concepts in Java, which is a long and important topic. You’ve learned the basics of the class, and the difference between a class and an object. You also knew about the construction functions, their purpose, and basic usage. In the next article we are delving more in the object oriented concepts of Java so stay tuned.

1 COMMENT

  1. Wonderful points you have mentioned here. It’s really a great and useful piece of information. I’m glad that you shared this helpful information with us. I shared this with my friends and my followers as its really great stuff to share! Keep up the good work.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -