Home Java Tutorial Classes and Objects in Java

Classes and Objects in Java

by anupmaurya
5 minutes read

In this article you’ll learn about Classes and Objects in Java, Object Creation,Class Creation, Nested Class and more.

Object

In object-oriented programming everything comes under the object and class.

  • Object is a physical entity which has states and behavior e.g. dog, car, fan, bicycles, pen etc.
  • Dogs have states (color, name, breed) and behaviors (barking, eating etc)
  • Cars have states (color, brand, speed, gear) and behavior (changing gear, applying breaks).

Object Creation

  • Creation of an object is also called as instantiation of an object.
  • The new operator is used to create an object of a class.

Example

Employee emp = new Employee ( );

Where emp is an object of Employee class. Employee ( ) is the default constructor of that class.

Example: Program for use of new keyword

class Employee
{
     String companyName = "Techarge";
     public Employee(int empId, String name)
     {
          System.out.println("Employee Id : "+empId+"\nEmployee Name : "+name);
     }
     public void EmpMethod()
     {
          System.out.println("Company Name : "+companyName);
     }
     public static void main(String args[])
     {
          Employee emp = new Employee(101,"ABC");
          emp.EmpMethod();
     }
}

Output:

Employee Id: 101
Employee Name: ABC
Company Name: Techarge

Classes

  • Class is a blueprint to create different objects.
  • It encapsulates the object, class, methods, constructor, block and data member.
  • class keyword is used to declare a class.

Syntax

class ClassName
{
     datatype variablename ;
     datatype methodname( parameter )
     {
          method – body
     }
}

Example: Sample of a Class

public class Employee
{
     String Name;
     int EmpId;
     int age;
     double salary;
     void empDept()
     {
     }
     void empProject()
     {
     }
}

Nested class

Defining a class within another class, it is called as nested class.

Syntax

class OuterClass
{
     class NestedClass
     {
     }
}

Inner class

  • Non-static nested class is known as inner class.
  • Java inner class provides code optimization.
  • Inner class can be declared private.

Example: Sample program for inner class

class OuterDemo
{  
     private int id = 101;
     private String name = "CareerRide Info";  
     private class InnerDemo
     {  
          void method()
          {
               System.out.println ("Id : "+id+" Name : "+name);
          }  
     }  
     public static void main(String args[])
     {  
          OuterDemo outer=new OuterDemo();  
          OuterDemo.InnerDemo innner=outer.new InnerDemo();  
          innner.method();  
     }  
}

Output:

Id : 101 Name : CareerRide Info

Local class

A class which is declared inside the method body is known as local class.

Example: Sample program for local class

class OuterDemo
{  
     private int a = 40;
     private int b =50;
     void display()
     {  
          int c = 60;  
          class LocalDemo
          {  
               void method()
               {
                    System.out.println("a + b + c = "+(a+b+c));
               }  
          }  
          LocalDemo l = new LocalDemo();  
          l.method();  
     }  
     public static void main(String args[])
     {  
          OuterDemo obj=new OuterDemo();  
          obj.display();  
     }  
}

Output

a + b + c = 150

Anonymous class

A class is declared without name is known as anonymous class. The instance of anonymous class is created at the time of its object creation.

It can be created by two ways.

1. Using class variable
2. Using interface

Example: Sample program for anonymous class

abstract class Bike
{
     abstract void speed();  
}  
public class AnonymousDemo
{  
     public static void main(String args[])
     {  
          int s = 50;
          Bike bike=new Bike()
          {  
               void speed()
               {
                    System.out.println ("Speed of Bike : "+s+" Km/h" );
               }  
          };  
          bike.speed();  
     }  
}

Output

Speed of Bike : 50 Km/h

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.