Java Object-Oriented Programming Tutorial on Concepts & Syntax with Stone River eLearning

Object Oriented Programming involves classes and objects and uses these two concepts to execute programs instead of relying solely on actions and logics. Object Oriented Programming or OOP; makes the job of a programmer easier and brings synchronization to various steps of a program. Given the liberty of declaring classes and sub-classes, OOP makes the overall code more organized, structured and reusable.

While C++ is the most basic programming language that deploys OOP concepts, JAVA comes out as the programming language which has really utilized the concepts of Object Oriented Programming to the core and related them to the real world scenarios. Since JAVA is used extensively across the world; it has given more significance and popularity to the OOP concepts.

 

So, how does Java deploy the OOP concepts?

Unlike C++, JAVA is a pure Object Oriented Programming language because one cannot run even the simplest of JAVA codes without including classes and objects in it. All the methods and variables in a JAVA code, including the main method, are enclosed within a class and then an object of that particular class is used to access the required method or the variable.

 

What is a class?

As explained in the video tutorial, a class is a prototype which can be replicated in the form of an object. In easier terms, a class is the name given to the bundle of objects, grouped together, under one single name. Eg: There are hundreds of different varieties of fruits available in the world, Apple, Orange, Banana, they are all fruits. Therefore, Fruit here is a class with Apple, Orange, Banana etc as the different objects or instance of the class.

 

What is an object?

An object is an instance of a class. In the class called fruits, banana, apple etc were all objects. In a more technical perspective; an object is an entity in the real world that has both, a state and a behavior.

How are classes and objects implemented in JAVA?

Let us discuss the example that is given in the video tutorial

Eg:


public class Main

{

public static void main(String[] args)

{

class MyClass

{

String name;

int age;

}

MyClass object1 = new MyClass();

object1.name= “Ollie”;

object1.age = 18;

MyClass object2 = new MyClass();

object2.name = “ Mark”;

object2.age = 21;

}

}

Now, Main, MyClass are the two classes declared in the program by the user. As you can notice from the example, the syntax of declaring a class in JAVA is the keyword ‘class’ followed by the name of the class to be declared.

 

Note:

Class name is case sensitive and therefore, MyClass( with an uppercase M and an uppercase C) is not the same class as myclass ( with both m and c in lowercase).

 

Points to Ponder:

  1. The methods and variables of a class are enclosed inside the curly braces after the declaration of the class.
  2. These methods and variables can be accessed as many number of times as required by using an object of this class.

object1 and object2 are the two objects to the class MyClass. The example highlights that a class can have more than one object, however, the name of the objects should not be the same.

 

Note:

  1. The syntax for the declaration of a class object is important as it describes a few important things about how JAVA operates.
  2. Taking note of the object creation, “ MyClass object1 = new MyClass(); ”, the name of the class whose object is to be created needs to be mentioned first, followed by the name of the object. The part after the “ = “ (equal to) sign, allocates memory to the object.
  3. Since JAVA works on dynamic memory allocation, the new keyword cannot be avoided as its omission will give a Null Pointer exception in the code. We will discuss more about it in the coming blog posts, but for now, it is important to note that we must follow the syntax for declaring classes and their objects.
  4. You can explicitly access a class variable or method from an object of the class, just like object1 and object 2 were used to access the variables name and age explicitly in the above example.
  5. The name of the JAVA file must be saved by the name of the class that has the main method in it. For the above example, the code must be saved as “ Main.java “ for it to run without any error.

Even though classes and objects are the most basic and comparatively the easier concepts of JAVA programming, but they are very fundamental and are used extensively in every JAVA code that is there in the world. Therefore, a good knowledge and understanding of this tutorial on OOP in JAVA, helps to build a platform for the concepts which will be discussed in the later sections.

 

Online Technology Courses Stone River eLearning

Comments