Object & Class

 

Object & Class

OOP’s stands for Object Oriented Programming. It is a concept of using objects in programming to implement real-world entities. OOP provides a clear syntax for our code making it easier to execute the code.

The key concepts in OOP include:

  • Object & Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

 

Object & Class

An object can be any real-world entity such as a book, cupboard, pen, paper, fan, etc. While a class is a group of objects with similar properties.

An object is an instance of a class while a class is a blueprint from which we create objects.

i. Different ways to create objects:

Let’s how we can create a class and from it, create an object.


Syntax to create object from class:

className objectName = new className();


Example:

public class Example1 {

    String name = "Maxon";
    int age = 20;
    
    public static void main(String[] args) {
        Example1 ex = new Example1();
        System.out.println("Name: " + ex.name);
        System.out.println("Age: " + ex.age);
    }
}


Output:

Name: Maxon
Age: 20

 

In the above example, first we create a class using the class keyword. We have named our class Example1. Now in order to create an object from Example1 class we will use the new keyword. 


new keyword allocates heap memory to object at run time.

 

We can even create multiple instances of the same class.


Example:

public class Example2 {

    String name = "Maxon";
    int age = 20;
    
    public static void main(String[] args) {
        Example1 ex1 = new Example1();
        Example2 ex2 = new Example2();
        System.out.println("Name: " + ex1.name);
        System.out.println("Age: " + ex2.age);
    }
}


Output:

Name: Maxon
Age: 20

This example is similar to the previous example, i.e. we get same output. But the difference is that, in this example, we have created two instances of the same class while in the previous example only one single instance was created.


Finally, we can program our code in one class and call it as an object in the main() method of another class. This helps us organize our code thus making it easy to use, detect errors, modify, etc.


Example3.java:

public class Example3 {
    String name = "Maxon";
    int age = 20;
}

 

 

Example4.java:

public class Example4 {

    public static void main(String[] args) {
        Example3 ex3 = new Example3();
        System.out.println("Name: " + ex3.name);
        System.out.println("Age: " + ex3.age);
    }
}


Output:

Name: Maxon
Age: 20

 

ii. Class attributes/fields:

Class attributes are the variables created inside a class.


Example:


public class Example3 {
    String name = "Maxon";
    int age = 20;
}

Here name and age are attributes or fields of class Example3.

 

We have already seen how to access these attributes in previous examples, i.e. we create object and access the attribute of that object.


Example:


public class Example1 {

    String name = "Maxon";
    int age = 20;
    
    public static void main(String[] args) {
        Example1 ex = new Example1();
        System.out.println("Name: " + ex.name);
        System.out.println("Age: " + ex.age);
    }
}


But the attributes declared in the class can be overridden in the main method().


Example:


public class Example1 {

    String name = "Maxon";
    int age = 20;
    
    public static void main(String[] args) {
        Example1 ex = new Example1();
        ex.age = 26;
        System.out.println("Name: " + ex.name);
        System.out.println("Age: " + ex.age);
    }
}


Output:

Name: Maxon
Age: 26


As we can see, we changed the value of age attribute in the main method and the output was affected by it. But what if we don’t want the main method to override any declared value?

This can be done using the final keyword. Essentially what final keyword does is that, once the attribute holds certain value, then it cannot be overridden.


Example:


public class Example1 {

    final String name = "Maxon";
    final int age = 26;
    
    public static void main(String[] args) {
        Example1 ex = new Example1();
        ex.name = "Kapil";
        ex.age = 24;
        System.out.println("Name: " + ex.name);
        System.out.println("Age: " + ex.age);
    }
}


Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The final field Example1.name cannot be assigned
    The final field Example1.age cannot be assigned
    at javaFiles/ObjectNClass.Example1.main(Example1.java:10)


iii. Class Methods:

Methods are a block of code that accept certain parameters and perform actions whenever they are called. Methods are always written inside a java class and can be called by simply referring to the method name.


In java we have public and static methods. So what is the difference between?


public methods are accessed by making objects of the class whereas static methods do not need objects to be accessed, we can directly accessed static methods.


Example:


public class Example5 {
    //public method
    public void fruits() {
        String fruits[] = {"Apple", "Banana", "Mango", "Strawberry"};
        System.out.println("Fruits:");
        for (int i=0; i<fruits.length; i++) {
            System.out.println(fruits[i]);
        }
    }
    
    //static method
    static void vegetables() {
        String vegies[] = {"Onion", "Potato", "Carrot", "Raddish"};
        System.out.println("Vegetables:");
        for (int i=0; i<vegies.length; i++) {
            System.out.println(vegies[i]);
        }
    }
    
    public static void main(String[] args) {
        Example5 ex5 = new Example5();        //need to create object
        ex5.fruits();
        System.out.println();
        
        vegetables();                        // no need to create object
    }
}


Output:


Fruits:
Apple
Banana
Mango
Strawberry

Vegetables:
Onion
Potato
Carrot
Raddish


As we can see, we have created two methods, inside which each of the method has array and for loop to print them. But public method needs an object to be declared in the main() method while we can directly use the static method inside the main() method.