Java: Introduction to OOPs (Part 2)

Java: Introduction to OOPs (Part 2)

- Vinayak Panchal

ยท

4 min read

In the last article, we got a brief introduction to OOP and its terminologies. In this article, we will understand OOP in depth along with creating custom classes, making multiple objects, assigning attributes, calling methods from custom classes, etc. If you haven't read Part 1, don't worry I will begin with a small introduction to OOP. Access Part 1 by clicking down below

Introduction

OOP stands for Object-Oriented Programming. Object Oriented Programming tries to map code instructions with the real world by making the code short and easy to understand. Procedural programming is about writing procedures or methods that perform operations on the data. Programmers wanted to map code instructions with the real world making the code short, clean, and easy to understand.

Classes and Objects

Class

A class is a template for objects. Think of it like a website where you can apply for your entrance exam. Many of us, must have given national-level entrance exams, wherein we visit the portal where we have to fill in our details such as name, qualification, Date of birth, etc. The details are not filled, this is a class.

Object

a class is a template for objects, and an object is an instance of a class. Continuing the above example, when we fill the form, write down our Name, Qualification, Date of Birth, etc. This filled form is an object. We can make many objects from one class like we have multiple candidates filling the same form. Memory is allocated after object instantiation.

Let us understand classes and objects through more examples:

INTRODUCTION (2).png

Here Class is Cars, which is a template and the objects are Tesla, BMW, Mercedes, and Lamborgini. When the individual objects are created, they inherit all the variables and methods from the class.

Java is an object-oriented programming language, hence everything in Java is associated with classes and objects, along with its attributes and methods.

Modeling problems in OOP

We will understand how to model problems using the below practice:

INTRODUCTION (3).png

A class is always a noun since it is a template and we use it to instantiate objects. Cars, Application forms, Fruits, Vegetables, etc are all classes that have their respective objects. The above analogy helps us to define classes. You might not understand its importance now(assuming you are just started learning oops) it will help you tremendously when you will get into mobile development and stuff.

Always think of attributes (the value we derive out of parameters) as adjectives and think of methods/functions as verbs. The method executes a certain block of code every time we call it, hence it is associated with the verb (action) here. Attributes are associated with adjectives because it defines the characteristics of classes, for example, filling in name, age, date of birth, etc in an application form makes it unique and yours.

Writing a custom class

Real world objects ---> Properties + Behaviour

Object in OOP ---> Attributes + Methods

Let us write our first custom class without creating methods. We create a custom class named Employee and declare datatypes. In the main method, we create a new object (Employee John = new Employee();) and set attributes using a dot and writing the variable name as shown in the below example.

package com.vinayak;
class Employee{ // access modifier is public by default
    int id;
    String name;
    int salary;
}

public class customClassinJava{
    public static void main(String[] args) {
        System.out.println("This is our custom class");
        Employee John = new Employee(); // Instantiating a new Employee Object
        //Setting attributes for John ๐Ÿ“Œ
        John.id = 45;
        John.name = "John Carter";
        John.salary = 12000;
        System.out.println(John.id);
        System.out.println(John.name);
        System.out.println(John.salary);

        Employee Peter = new Employee();
        // Setting attributes for Peter ๐Ÿ“Œ
        Peter.id = 13;
        Peter.name = "Peter Parker";
        Peter.salary = 25000;
        System.out.println(Peter.id);
        System.out.println(Peter.name);
        System.out.println(Peter.salary);
    }
}

Let us now create a custom class using methods. Creating and then calling a method makes us do the same thing using less code and help us understand code better, see the example below for better understanding:

package com.vinayak;
class Employee{ // access modifier is public by default
    int id;
    String name;
    int salary;
    public void printDetails(){
        System.out.println("My id is  " + id);
        System.out.println("My name is " + name);
        System.out.println("My salary is " + salary);
    }
}

public class customClassinJava{
    public static void main(String[] args) {
        System.out.println("This is our custom class");
        Employee John = new Employee(); // Instantiating a new Employee Object
        //Setting attributes for John ๐Ÿ“Œ
        John.id = 45;
        John.name = "John Carter";
        John.salary = 12000;
        printDetails();

        Employee Peter = new Employee();
        // Setting attributes for Peter ๐Ÿ“Œ
        Peter.id = 13;
        Peter.name = "Peter Parker";
        Peter.salary = 25000;
        printDetails();
    }
}

That's a wrap!

TheOfficeGIF.gif

Thank you for Reading my Article๐Ÿ“„ Hope it adds value to the community๐Ÿ’™

Follow me on Twitter : vinayakstwt ๐Ÿ•Š๏ธ

Follow me on Hashnode: VINAYAK PANCHAL ๐Ÿš€

ย