Object and Class in Java

Object-oriented programming is the concept of solving problems by breaking them into objects.Object-oriented programming is inspired from real world.All object-oriented programming languages provide mechanisms that help you implement the object-oriented model. Object-oriented programming language provides following advantages.

Advantages of OOPs

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

Now I am going to explain the terminology used in OOps in java language.

Class

It acts as template to define data members and methods for objects.A class is logical entity which acts as blue print for its objects.Class can be created using class keyword.

Here is simple class example to print “Hello World” message.

HelloWorld.java


public class HelloWorld{

  public static void main(String args[]){

  System.out.println("Hello World");

}
}

Constructor

Constructor is a method which invoked while we creating object.Constructor is used to initialized instance variables.If we don’t define any constructor compiler will create a default constructor. Constructor name should be name of class.


public class Product{
  public Product(){
   System.out.println("Constructor invoked");
  }
 }
 

Object

An object is an instance of a class.Object has its own data member and methods.In java objects are created in heap area.In java new keyword is used to create objects.


public class Product{
  private String name;
  public Product(String name){
  System.out.println("Constructor invoked");
  this.name =name;
 }
 public static void main(String args[]){
  Product p = new Product("Product A");
 }
}

Attributes/Data Members

Attributes or data members are the individual things that differentiate one object.Like Product object has price,color,qty attributes.Data members can be of two types.

Instance data member : Instance data member can be accessed through object.Instance data member initialized when the class is instantiated.These data members can be accessed from inside any method, constructor or blocks of that particular class.

Class data member : Class variables are declared with in a class, outside any method, using static keyword.They can be accessed with the name of class without creating objects.

Behavior/Methods

Behaviors or methods of an object determines the operations can be performed on data members.Like the Product object can have following methods.Methods can also be two types.

Instance Methods : Instance methods can only invoked using object of class.

Class Methods: Class methods can be accessed by class name without creating objects.

Here example to invoke methods of class using object.


public class Product{

private String name;

private int price;

//constructor

public Product(String name,int price)

{

this.name = name;

this.price=price;

}

public void display(){

System.out.println("Name:"+name);

System.out.println("Price:"+price);

}

public static void main(String args[]){

//Creating objects

Product p = new Product();

//Invoking method

p.display();

}

}

 

 

 

 

Leave a Reply