Object And Class



What Is a Class?

A class is the blueprint from which individual objects are created.

In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components.

In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.

Declaration Of Class

	class ClassName
	{
		//Declare Variable
		//Declare Functions
		
	}

	OR

	class Student
	{
		int rollno;
		String name;
		public void getDetails()
		{

		}
		public void showDetails()
		{

		}
	}

A class can contain any of the following variable types.

Local variables Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.

Instance variables Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.

Class variables Class variables are variables declared with in a class, outside any method, with the static keyword.



Points To Remember

  • Class forms the basis for object-oriented programming in Java.
  • A class can be used to create objects.
  • It defines a new data type.
  • A class is a template for an object, and an object is an instance of a class.
  • The data, or variables, defined within a class are called instance variables.
  • The methods and variables defined within a class are called members of the class.
  • A class creates a new data type that can be used to create objects. That is, a class creates a logical framework that defines the relationship between its members.
  • When you declare an object of a class, you are creating an instance of that class. Thus, a class is a logical construct. An object has physical reality.
  • Classes usually consist of two things: instance variables and methods.



What Is an Object?

Object is an instance of a class. Class is a template or blueprint from which objects are created. So object is the instance(result) of a class.

Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your dog, your desk, student, your television set, your bicycle.

An object has three characteristics:

state: represents data (value or variables) of an object.

behavior: represents the behavior (functionality or methods) of an object such as getDetails,showDetails,addition,subtraction etc.

identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But,it is used internally by the JVM to identify each object uniquely.

Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail).

Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.

Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions:

"What possible states can this object be in?" and "What possible behavior can this object perform?".

As you do, you'll notice that real-world objects vary in complexity. Your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune).

You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming.



Object Creation

In Java, the new key word is used to create new objects.

There are three steps when creating an object from a class:

Declaration: A variable declaration with a variable name with an object type.

Instantiation: The new keyword is used to create the object.

Initialization: The new keyword is followed by a call to a constructor. This call initializes the new object.

Example

public class Student{

   public Student(String name){
      // This constructor has one parameter, name.
      System.out.println("Name is :" + name ); 
   }
   public static void main(String []args){
      // Following statement would create an object ob
      Student ob = new Student("Ravi");
   }
}

Accessing Instance Variables and Methods

Instance variables and methods are accessed via created objects. To access an instance variable the fully qualified path should be as follows

/* First create an object */

Student ob = new Student( "Ravi");

/* Now call a variable as follows */

ob.variablename;

/* Now you can call a class method as follows */

ob.MethodName();

Example

public class Student
	{
   String name;

   public Student(String name){
      // This constructor has one parameter, name.
      this.name=name
   }
   public static void main(String[] args){
      /* Object creation */
      Student ob = new Student("Ravi");

      /* You can access instance variable as follows as well */
      System.out.println("Name Is:" + ob.name); 
   }
}

Source file declaration rules

  • There can be only one public class per source file.
  • A source file can have multiple non public classes.
  • The name of the source file should be same as public class name.
  • First letter of class name should be Capital
  • If the class is defined inside a package, then the package statement should be the first statement in the source file.
  • If import statements are present then they must be written between the package statement and the class declaration. If there are no package statements then the import statement should be the first line in the source file.
  • Import and package statements will imply to all the classes present in the source file. It is not possible to declare different import and/or package statements to different classes in the source file.
  • Classes can be public or default and there are different types of classes; abstract classes, final classes, etc.
  • Apart from the above mentioned types of classes, Java also has some special classes called Inner classes and Anonymous classes.