Objects and Classes in Java

Java is an Object-Oriented  Programming Language. Features of Object Oriented  which is included in java programming

  1. Polymorphism

2. Inheritance

  •      3. Encapsulation
  •      4. Abstraction
  •      5 . Classes
  •      6. Objects
  •      7. Instance
  •       8. Method
  •      9.Message Parsing

 

Here  we will look into  – Objects and classes in java.

  • Object − When Java Runtime Machine encounters the new keywords, it uses the appropriate class and makes it object which is also called an instance of a class. An instance of a class has its own state and behavior. An instance of a class has access to behavior(methods) .
  • Class − A  template that describes the  kind of state and behavior that the objects of its type support.

Objects in Java

Let us now look deep into what are objects. If we consider the real-world, we can find many objects around us, cars, dogs, humans, etc. objects in java have a state and a behavior.

lets talk about  dog class, then dog class has its state which are – name, breed, color, and the behavior of dog class – wagging ,barking, the tail, running.

Object is software world have the same characteristic as Object in real world.

 

So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods.

Classes in Java

A  template that describes the  kind of state and behavior that the objects of its type support..

Following is a Example of a class.

Example

public class Dog {
   String breed;
   int age;
   String color;

   void barking() {
   }

   void hungry() {
   }

   void sleeping() {
   }
}

A java class can contain following type of variable.

  • Variables  Local − A Variables  which is declared or define inside a methods or a constructors or blocks are called local variables.
  • Variables Instance −A variable which is inside a class but outside of its method are called instance variable. This type of variable will be initialized when class is instantiated.
  • Class variables −variables declared  with the static keyword.

 

Let look about few important things about the java classes

Constructors

 

When an object is being created for java class , a constructor will be called by the JVM, if a class does not have any constructor then JVM will provide the default constructor .

lets take a look at the Constructor −

public class Vipin{
   public Vipin() {
   }

   public Vipin(String name) {
      // This constructor has one parameter, name.
   }
}

 

Creating an Object

As mentioned previously, a class provides the blueprints for objects. So basically, an object is created from a class. In Java, the new keyword is used to create new objects.

Steps 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.

 

creating an object −

 

public class Puppy {
   public Puppy(String name) {
      // This constructor has one parameter, name.
      System.out.println("Passed Name is :" + name );
   }

   public static void main(String []args) {
      // Following statement would create an object myPuppy
      Puppy myPuppy = new Puppy( "tommy" );
   }
}

If we compile and run the above program, then it will produce the following result −

Output

Passed Name is :tommy

Accessing Instance Variables and Methods

Instance variables and methods are accessed via created objects. To access an instance variable, following is the fully qualified path −

/* First create an object */
ObjectReference = new Constructor();

/* Now call a variable as follows */
ObjectReference.variableName;

/* Now you can call a class method as follows */
ObjectReference.MethodName();

Example

This example explains how to access instance variables and methods of a class.

 

public class Puppy {
   int puppyAge;

   public Puppy(String name) {
      // This constructor has one parameter, name.
      System.out.println("Name chosen is :" + name );
   }

   public void setAge( int age ) {
      puppyAge = age;
   }

   public int getAge( ) {
      System.out.println("Puppy's age is :" + puppyAge );
      return puppyAge;
   }

   public static void main(String []args) {
      /* Object creation */
      Puppy myPuppy = new Puppy( "tommy" );

      /* Call class method to set puppy's age */
      myPuppy.setAge( 2 );

      /* Call another class method to get puppy's age */
      myPuppy.getAge( );

      /* You can access instance variable as follows as well */
      System.out.println("Variable Value :" + myPuppy.puppyAge );
   }
}

If we compile and run the above program, then it will produce the following result −

Output

Name chosen is :tommy
Puppy's age is :2
Variable Value :2

Source File Declaration Rules

 

  • 1. A Source file can have only one public class.
  • 2. Once source file can have multiple classes in it. if there is a public class in it then the file name must match with the name of the public class in it.
  • 3. Person{} then the source file should be as Person.java.
  • 4. If there is any package defined in a class then the package statement must be the first line of statement in the source file.
  • 5. The import statement must come after the package statement but before the class declaration.

 

6. If there is no package statement then import statement must be the first line of statement in class declaration.

7. If there is no public class in a source file then it can be names as any name with dot java extension.