Classes and objects in java

Let’s learn classes and objects in java.

Classes and objects in java

Class is a blueprint. Class is the collection of objects. To create a class in java use keyword ‘class’.

For example:

Class – Animal

Object – tiger, lion, cheetah etc.

Syntax:

access-modifier class ClassName
{
   // methods
   // constructors
}

In the below example I have created a class by name Employee. This class has variable salary of datatype integer.

class Employee
{
   int salary = 50000;
}

Basically a class consists of intrinsic elements, namely,

Another important element of a class is constructor. Constructor is used to initialize objects.


Types of classes in java

Below are types of classes in java

  • POJO Class
  • Static class
  • Concrete Class
  • Abstract class
  • Final Class
  • Inner Class
  • Nested Inner class
  • Method Local inner classes
  • Anonymous inner classes
  • Static nested classes

POJO class: POJO class is nothing but “Plain Old Java Object”. This class consists of private variables and getter and setter methods.

POJO class may override some methods from Object, for example, equals() method.

Let’s see an example of POJO class

class POJODemo
{
   private int number = 500;
   public int getNumber()
   {
      return number;
   }
   public void setNumber(int number)
   {
      this.number = number;
   }
}
public class POJOExample
{
   public static void main(String[] args)
   {
      POJODemo obj = new POJODemo();
      System.out.println(obj.getNumber());
   }
}


Output:

500


Static class: A static class is class which is described using “static” keyword. We cannot create object for a static class.

Also read – static keyword in java

Static members belongs to class instead of specific instance. Let’s see an example,

class StaticExample
{
   // static method
   static void display()
   {
      System.out.println("in static method");
   }
   public static void main(String[] args)
   {
      // calling display() method without creating object
      display();
   }
}


Output:

in static method


Concrete class in java

A concrete class is a class which has implementation of all its methods and which does not have abstract method.

Also read – methods in java

Concrete class can extend an abstract class or parent class or implement interface if it implements all its methods.

Let’s see an example of concrete class.

public class ConcreteExample
{
   static int multiply(int a, int b)
   {
      return a * b;
   }
   public static void main(String[] args)
   {
      int m = multiply(5, 10);
      System.out.println("Product: " + m);
   }
}


Output:

Product: 50


Click here to learn abstract class.


Final class: A class, method or a variable declared with keyword “final” is called a final class.

A class declared with “final” keyword cannot be inherited or extended by any subclasses.

The main use of final class is it cannot be extended or inherited and the other use is to create immutable class like String class.

Let’s see an example on final class.

final class ParentClass
{
   void print()
   {
      System.out.println("print() method of ParentClass.");
   }
}
class ChildClass extends ParentClass
{ 
   // Compile-time error - The type ChildClass cannot 
   // subclass final class ParentClass
   void print()
   {
      System.out.println("print() method of ChildClass.");
   }
}
public class FinalExample
{
   public static void main(String[] args)
   {
      ChildClass obj = new ChildClass();
      obj.print();
   }
}


Output:

The type ChildClass cannot subclass the final class ParentClass.


Click here to learn Inner Classes, Nested Inner class, Method Local inner classes, Anonymous inner classes and Static nested classes.


classes and objects in java: object definition in java?

Object is an instance of a class. Object is real world entity. Object occupies memory.

Also read – variables in java

Object consists of identity, state or attribute and behaviour. Here’s realtime example of classes and objects.

classes and objects in java

Classes and objects in java: Creating an object

There are five ways to create object in java.

  • using new keyword.
  • using newInstance() method.
  • using clone() method.
  • by deserialization.
  • by using factory methods.

In this post we are going learn to create an object using new keyword. The remaining four ways will discuss in future posts.

read more about – java class constructor

Here’s how to create object in java with example.

There are three steps to create object in java
1. Declaration: For example, Bird sparrow;
2. Instantiation: use "new" keyword, 
   sparrow = new Bird();
3. Initializaition: in initializaition use constructor.

Syntax for object creation:

Bird sparrow = new Bird();



classes and objects in java: multiple objects

Meanwhile we can create multiple objects of class Employee.

class Employee
{
   int salary;
   public static void main(String[] args) 
   {
      // object 1
      Employee obj1 = new Employee();
      // object 2
      Employee obj2 = new Emplyee();
      System.out.println(obj1.salary);
      System.out.println(obj2.salary);
   }
}



Initialize an object in java

There are three ways to initialize an object in java.

  • by reference variable.
  • by method.
  • by constructor.

Let’s see an example to initialize an object in java – by reference variable.

import java.util.*;
class Employee
{
   String name;
   int age;
   public static void main(String[] args)
   {
      Employee emp = new Employee();
      emp.name = "Virat";
      emp.age = 23;
      System.out.println(emp.name + " " + emp.age);
   }
}


Output:

Virat 23


Click here for example to initialize an object in java by constructor.


Here’s is an example to initialize an object in java by using method.

class Employee
{
   String empName;
   int empAge;
   void initialize(String name, int age)
   {
      empName = name;
      empAge = age;
   }
   void print()
   {
      System.out.println(empName + " " + empAge);
   }
   public static void main(String[] args)
   {
      Employee emp = new Employee();
      emp.initialize("virat", 23);
      emp.display();
   }
}


Output:

virat 23