Java Object and Classes

Published by Admin on

Java Object and Classes - intechnologies.in

Java is an Object-Oriented programming Language. The Java Object and Classes which aims to implement real-world entities.

Java Class

A class is a user defined template from which objects are created. It describes the state (fields) and behavior (methods) that are common to all object of one type.
State– It describes the attributes of objects.
Behavior– It describes the method of objects..

For Ex. 1) Fan is an object
State: On or Off
Behavior: Turn On or Turn Off
2 ) Car is an object
States: Current Gear, Four Wheels
Behavior: Changing Gears, Braking and Accelerating

Syntax of class declaration

[public] class ClassName [extends ClassName]
[implements Interface1, Interface2,...]
{
 //Variable declaration
 //Constructor & Method declaration
 //main method
 //Inner classes
 //Inner interface
}

Create a Class – You need to define a class for creating an objects. A class is a template for the object.

public class Employee{
   // instance variable
   private int employeeNo;
   String employeeName;
   //Constructor
   Employee(){
      employeeNo=0;
      employeeName="Bla Bla Bla";
   }
   // method
   public void putData() {
       System.out.println("Employee No: "+employeeNo+" \n Employee Name:"+employeeName);
   }
}

The class has two variable named employeeNo and employeeName and one methods outData(). These variables and methods defined within a class are called members of the class.

Java Class Design Hint

i) Always keep data private- This is the first & foremost: Doing anything else violates encapsulation . A user need to write accessor or mutator method for the private instance field.

ii) Always initialize data- Java Won’t initialize local variable, but it will initialize instance fields of objects. Don’t rely on data but initialize the variable explicitly.

iii) Don’t use to many basic types in a class-  The idea is to replace multiple related uses of basic types with other classes. This keep classes easier to understand and easier to change.

iv) Not all fields need individual field accessor and mutator, For ex. A user need to get and set an employee salary, but the hire-date of employee need not to be changed once the object is constructed.

v) Use a standard form for class definition- A user should write the contains of classes in the following order.

public features
[friendly] package scope features
private features

Each section will contain contents in the following order

instance methods
static methods
instance fields
static fields

vi) Breakup classes with too many responsibility -Break up a class as per need.

vii) Make the name of classes & method reflect their responsibilities- A good conversion that a class name should be a noun( ordered) or noun precided by an objective by (Rush Over) & method name should be a verb

Java Objects

An object is nothing but an instance of a class that represents the real life entities because that contains real values of variables. For example, suppose Animal is a class then Dog, Cat, Horse can be considered as objects of Animal class.

Here is syntax, How can we create objects in Java:

className object = new className();

Here, we are using the constructor className() to create the object. when an object of the class is created its constructor invoked automatically. Constructors have the same name as the class and are similar to methods. Constructors doesn’t have any return type.

//For example we can create object of Employee class
public static void main(String args[]){
Employee emp1=new Employee(); 
/* emp1 is called as object of Employee class and e also called instance variable.
Employee() is constructor that initialize the fields with default values  */
e.putData();
}

The Output will be shown as

Employee No: 0
Employee Name: Bla Bla Bla

In this article, we understand Java Classes and Object, If you like this article please subscribe our InTechnologies.in


Question :-

  • What is Class?
  • What are Java Classes and Objects?
  • What is Objects?
  • Does Constructor creates the object ?

Facebook
Twitter
Pinterest
LinkedIn
Instagram
Follow by Email
YouTube
Telegram
RSS
WhatsApp