Core Java – Java Class and Objects

Posted by

Java is an object oriented programming language and its is driven based on Java Classes and Object instantiation. The tutorial provides how to create Java Classes and instantiate object on the class.

What is Object in Java ?

An Object is a group of related items grouped together and can have characteristics like state and behavior.

Lets consider Room as an Object , then Blackboard, Table, chair, Chalk are considered as its State (attributes /fields) and totalChairs and TotalTables are considered as behaviour (methods) which defines the Room as an Object.

Similarly a furniture being an object could have attributes like table, chair, dine table, stool, sofa,etc.

Example -1

  • Object – Room
  • State – Blackboard, Table, chair, Chalk
  • Behavior – Seat Capacity, TotalChairs, TotalTables

Exmaple -2

  • Object -SwtichBoard
  • State – On or Off
  • Behavior – TurnOff / TurnOn

What is class in Java ?

Class in java acts as the Object constructor or we can say that its the blueprint which defines the attributes for the created object.

The Class contains the variables (State) and methods . The below given is the class with name Furniture and has variables

/**
 *  Oracleappshelp.com - Java Basics Tutorial
 *  Program - Class Creation and Object Instantiation Example
 *   */
package core.java.oracleappshelp.basics;
public class Furniture {
	String strTable;
	String strChair;
	String strSofa ="Recliner";
	String strDineTable;
	public static void main(String[] args) {
		// Object Creation for the Class Furniture 
		Furniture furnitureObj = new Furniture();
		// Basic Output 
		System.out.println("This is basic furntiure class..");
		
		// Object instantiation and execution
		System.out.println("The Sofa Type is :" +  furnitureObj.strSofa);
	}
}
Output: 
This is basic furntiure class..
The Sofa Type is :Recliner

Now consider the modified class which has varibles with setter and getter methods and a Method invocation

/**
 *  Oracleappshelp.com - Java Basics Tutorial
 *  Program - Class Creation and Object Instantiation Example
 *   */
package core.java.oracleappshelp.basics;
public class Furniture {
	
	String strTable;
	String strChair;
	String strSofa ="Recliner";
	String strDineTable;
	public static void main(String[] args) {
		// Object Creation for the Class Furniture 
		Furniture furnitureObj = new Furniture();
		// Basic Output 
		System.out.println("This is basic furntiure class..");
		
		// Object instantiation and execution
		System.out.println("The Sofa Type is :" +  furnitureObj.strSofa);
		furnitureObj.setSofaType();
	}
	
	public void setSofaType()
	{
	    this.setStrSofa("3 seater sofa");
	    System.out.println("The Sofa Type is :" +  this.getStrSofa());
	}
	
	public String getStrTable() {
		return strTable;
	}
	public void setStrTable(String strTable) {
		this.strTable = strTable;
	}
	public String getStrChair() {
		return strChair;
	}
	public void setStrChair(String strChair) {
		this.strChair = strChair;
	}
	public String getStrSofa() {
		return strSofa;
	}
	public void setStrSofa(String strSofa) {
		this.strSofa = strSofa;
	}
	public String getStrDineTable() {
		return strDineTable;
	}
	public void setStrDineTable(String strDineTable) {
		this.strDineTable = strDineTable;
	}
}
Output: 
This is basic furntiure class..
The Sofa Type is :Recliner
The Sofa Type is :3 seater sofa