Object-oriented Programming (oop)

What is it? And Why it is important? PART-1 (JAVA)

Object-oriented Programming (oop)

Hey, my name is Aniketh Paul and today I will be sharing Basic object-oriented programming concepts which are not only necessary in real-world implementation but also makes us think in terms of objects and classes and help us write cleaner code.

Today we will cover static, packages, classes, and abstract topics and in the next blog, we will cover more advanced concepts.

Static Method

When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. Let's understand this in terms of main() in java. Remember we write this Public static void main(String[] args) when we used to call the main function in a java program? That is it. Here, main() is declared as static because it must be called before any objects exist. The static method in JAVA is a method that belongs to the class and not to the object. A static method can access only static data. It cannot access non-static data (instance variables.)

public class Human {

    String message = "Hello World";

    public static void display(Human human){
        System.out.println(human.message);
    }

    public static void main(String[] args) {
        Human Aniketh = new Human();
        Aniketh.message = "Aniketh's message";
        Human.display(Aniketh);
    }

}

Packages

Packages are containers for classes. They are used to keep the class namespace compartmentalized. For example, a package allows you to create a class named List, which you can store in your own package without concern that it will collide with some other class named List stored elsewhere. Packages are stored in a hierarchical manner and are explicitly imported into new class definitions.

To create a package -> package MyPackage;

Java uses file system directories to store packages. For example, the .class files for any classes you declare to be part of MyPackage must be stored in a directory called My package. Remember that case is significant, and the directory name must match the package name exactly.

Classes

A Class is a template for an object, and an object is an instance of a class. A class creates a new datatype that can be used to create objects. When you declare an object of a class, you are creating an instance of that class. Thus, a class is a logical construct.

Box b1 = new Box();
Box b2 = b1;
b1 and b2 will both refer to the same object. The assignment of b1 to b2 did not allocate any memory or copy any part
of the original object. It simply makes b2 refer to the same object as does b1. Thus, any changes made to the object
through b2 will affect the object to which b1 is referring since they are the same object.
When you assign one object reference variable to another object reference variable, you are not creating a copy of the
object, you are only making a copy of the reference.

Abstract

Sometimes you will want to create a superclass that only defines a generalized form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details. Such a class determines the nature of the methods that the subclass must implement. You may have some methods that must be overridden by the subclass in order for the subclass to have any meaning. In this case, you want some way to ensure that a subclass does, indeed override all necessary methods. Java's solution to this problem is the abstract method.

abstract type name(parameter-list);

Any class that contains one or more abstract methods must also be declared abstract.

  • There can be no objects of an abstract class.
  • You cannot declare abstract constructors or abstract static methods.
  • You can declare static methods in abstract class.

Did you find this article valuable?

Support aniketh paul by becoming a sponsor. Any amount is appreciated!