Class and objects in java

Defining Class


A group of objects that has common properties. It is a template or blueprint from which objects are created Classes are the fundamental building blocks of a Java program.

A class in java can contain:

•    data member
•    method
•    construtor
•    block
•    class and interface

A class declaration is made up of the following parts:


•    Modifiers
•    Class name
•    Keywords
•    Class body within curly brackets {}


What does Java Class Consist?


    When we create class in java the first step is keyword class and then name of the class or identifier we can say.

    Next is class body which starts with curly braces {} and between this all things related with that class means their property and method will come here.

Example: Defining a Class in Java
 

public class MyClass {
}


  What are members of Class?

The following are the members of a class:
•    A) Field
•    B) Method


Field: field is nothing but the property of the class or object which we are going to create.

Example:

 If we are creating a class called computer then they have property like
model, mem_size, hd_size, os_type etc   

Class computer {
//fields of computer class
Public model;
Public mem_size;
Public hd_size;
Public os_type;
}


Method: method is nothing but the operation that an object can perform. It define the behavior of object how an object can interact with outside world For example startMethod (), shutdownMethod ().

Example:

•    The class name is computer
•    Field names are model, mem_size, hd_size, os_type
•    Methods are startMethod , shutdownMethod

Class computer {
//fields of computer class
Public model;
Public mem_size;
Public hd_size;
Public os_type;

//method to start the computer
Public void startMethod(){
}

//method to start the computer
Public void shutdownMethod(){
}
}


Real world example of Class in Java Programming:
In real world if we want to understand about the class everything of same quality  can be visualize as a class

Example:

we can say vehicle .
The entire vehicle will make one class they have the property like no_of_wheels, color, model, brand etc.now we can think changeGear () and speedOfvehicle (), applyBreak () etc as a method on that class

In short in java every problem we get solution can be think in terms of class and object.

Java class example:

•    Stock is called Class
•    commodity and  price are field
•    buy() and sale() are two methods defined inside class

Class Stock {
Public commodity;
Public price;
Public void buy (int no_of commodity) {}
Public boolean sale () {}
}



  INTRODUCTION TO JAVA OBJECTS

Defining Object
Object is an instance of a class created using a new operator. The new operator returns a reference to a new instance of a class. This reference can be assigned to a reference variable of the class. The process of creating objects from a class is called instantiation.

Example:
•    To access elements of Class you need to create an instance of class Stock
•    To create objects of a stock class, you use the new keyword.

Stock tStock = new Stock();
For calling method of Stock just call by using instance.
tStock.buy(1000);
tStock.sell();

  .java Files

The above class definition should be put in its own file named MyClass.java. Java files should be named the same as the name of the class they contain, with the .java as file name extension. Make sure you keep the same uppercase and lowercase characters from the class name in the file name too.
Example 1: Creating and using instances of the Employee class to print the employee name, ssn, emailaddress and yearOfBirth