Jumping into OOPs (Java 101-4)🧬💻

Jumping into OOPs (Java 101-4)🧬💻

Type of Language📚🗺️

  1. Procedural Language - The user has to specify the procedures which are the 'How to do' part and also the 'What to do' part of the code to completely execute it. Examples - Java, Python, BASIC, C etc.

    While Non-Procedural language is where u only specify the 'What to do' part making the semantics easier to understand. It is also knowns as applicative language. Examples - SQL, Prolog etc.

  2. Functional Language - In this language, the code is written in pure mathematical function because of which it doesn't modify variable but produces a new one as output and thus are helpful in places where we have to deal with a huge set of data. Examples - Python, Javascript, Erlang etc.

  3. Object-Oriented Language - This language revolves around the object which is simply code + data. Because of the usage of objects, it becomes very easy to be maintained, debug, reuse and develop. Examples - Java, Python etc.

NOTE - A programming language is not restrained to be one type of language, it could be any one of them at the same time. For eg. Python follows Procedural, Functional and Object Oriented while C++ & Java are Procedural + object-oriented.


Object-Oriented Language Theory

Object📦🔍

The object is the basic unit of an Object-Oriented language and it is a real entity that occupies space in your HDD/SDD. Object consists of 3 things which are -

  1. Identity - unique name of an object helping it to interact with another object.

  2. State - Property/Attribute of an Object.

  3. Behaviour - It is the response that an object would give when interacting with another object. It is usually represented by methods of an object.

Classes🏫

Class is a set of objects which shares common characteristics/behaviour and common properties/ attributes. It is not a real-world entity as it is just a template or blueprint or prototype from which objects are created. So, as it is not a real entity it does not occupy memory.

//Adding two values by calling a method

class Calculator    // is a blueprint
{
    //creating a variable
    int a;
    //creating a method
    public int add(int n1, int n2)
    {
        int r= n1+n2;
        return r;
    }
}

public class Demo
{
    public static void main(String[] args) 
    {
        int num1=28;
        int num2=36;
        //calc.add(); If i open this line it will give error as it is not in Demo class and thus can't find it
        //Calculator calc; will also not work as it is acting as a reference but there is still no real object consuming some space.
        //this is how we create an object with the help of 'new'
        Calculator calc = new Calculator();

        int result = calc.add(num1,num2); //accepting return value from calc
        System.out.println(result);
    }
}

Methods🛠️📚

Methods are collections of statements that perform a particular task and then return the result to the caller. It helps us in reusing code as we do not have to rewrite it again when needed in different places in the code. Thus helping us in the Reusability and optimization of a code.

Basic Syntax

<access_modifier> <return_type> <method_name>( list_of_parameters)
{
    //body
}
  1. Access modifier - It defines that from where the method can be accessed in our application. In Java, there 4 types of access specifiers.

    • public: It is accessible in all classes in your application.

    • protected: It is accessible within the class in which it is defined and in its subclass/es

    • private: It is accessible only within the class in which it is defined.

    • default: It is declared/defined without using any modifier. It is accessible within the same class and package within which its class is defined.

Note - It is optional in syntax.

  1. Return Type - The data type of the value returned by the method or void if does not return a value. It is Mandatory in syntax.

  2. Method Name - Give a unique identity so that we could call it.

  3. Parameter List - Comma-separated list of the input parameters is defined, preceded by their data type, within the enclosed parenthesis. If there are no parameters, you must use empty parentheses () that's why it is Optional in syntax.

Example

class computer
{
    public void playmusic() //It will execute the code without returning anything
    {
        System.out.println("Music Playing...");
    }

    public String getmeapen(int cost)
    {
        if(cost>=20)
            return "PEN";  
      //else
        return "nothing";  //there is no need of else statement because if the above statement is true,because of 'return' it will just pass the if statement ignoring this one but if above statement false this 'return' would get returned to caller.
    }
}

public class method 
{
    public static void main(String[] args) 
    {
        //int cost=20;
        computer comp = new computer();  //making a new object
        comp.playmusic();
        String str = comp.getmeapen(2);
        System.out.println(str);
    }
}

Method Overloading

Method Overloading allows us to allot the same name to different methods which can differ by the number of input parameters or type of input parameters, or a mixture of both.

class calculator
{
    public int add(int n1,int n2,int n3)
    {
        return n1+n2+n3;
    }
    public int add(int n1, int n2)
    {
        return n1+n2;
    }
    public double add(double n1, int n2)
    {
        return n1+n2;
    }
}

public class metover
{
    public static void main(String[] args) 
    {
        calculator cal = new calculator();

        int r1=cal.add(24,353,3);
        int r2=cal.add(35,44);
        double r3=cal.add(34.35,333);

        System.out.println(r1);
        System.out.println(r2);
        System.out.println(r3);
    }
}

Conclusion📝🎓

Well, as time flies, our blog is getting infused with even more code! But hey, who needs lengthy paragraphs in English when we can jazz things up with some mind-blowing examples instead? 😎🚀 Get ready for a coding extravaganza! 💻🎉

Again thanks for joining me today, guys! 🙌 I hope you learned something new from my tech blog. 🤓 Until next time, take care and keep exploring the amazing world of technology! 🚀💻.

Also, give a heart button if u like it.😁

Did you find this article valuable?

Support Indrajit Mandal by becoming a sponsor. Any amount is appreciated!