Class , Objects And Methods in Java | Java for beginner | Java | Java Programming Language | OOP.

Introduction

Java is a true object-oriented language and therefore the underlying structure of all Java programs  is Classes. Anything we wish to represent in Java program must be encapsulated in a class that defines the state and behaviour of the basic program components known as Objects.

Classes create objects and objects uses method to communicate between them. That is all about object oriented programming.

Classes provide a convenient method for packing together a group of logically related data items and function that works on them. In Java, the data items are called fields and the function are called Methods. 

Calling a specific method in object is described as sending the object in message.


Creating a class

A class is a user defined data type with a template that serves to define its properties. 

Once the class type has been defined, we can create in open "variables"  in close of that time using a declaration that are similar to the basic type declaration. 

In Java, these variables are term as instance of classes, which are actually objects.

The basic form of a class definition is


Class classname [extends superclassname]
{
    Fields declaration;
    Methods declaration;
}



Classname and superclassname are any valid Java identifiers. the keyword extends indicates that the properties of the superclass name classes are extended to the classname class.


This concept is known as inheritance.

Example:-

class BasicProgram //this is class
{
    public static void main(String[] args) // main class where your program runs
    {
       System.out.println("*****Hello World*****");
    }
}



Creating object

An object in Java is essentially a block of memory that contains face to store all the instance variable.
Creating an object is also referred to as instantiating an object. Objects in Java are created using the new operator.

The new operator creates an object of the specified classes and returns a reference to that object.Here is a example of creating an object of type Rectangle.

Rectangle object = new Rectangle ();



Method declarations

Methods are declared inside the body of the class but immediately after the declaration of instance variables.


General form of method declaration is

type methodname (parameter-list)
{
//Method body;
}


Method declaration of four basic parts:-

  1. The name of the method (methodname).
  2. The type of the value the method Returns.(type)
  3. List of parameters.(parameter-list)
  4. The body of the method.
The type specifies the type of value the method would return, it may be int, float ,double and void if method that does not return any value.The parameter list is always enclosed in parentheses.

Example:-


Class Rectangle
{
    Int length;
    Int width;
    Void getdata (int x,int y) // method declaration
    {
       length=x;
       width=y;
    }
}




Example Including all Class , Object and Method.


package demo;

public class demopractical
{
   void display()
   {
      System.out.println("Java Is OOP Language");
   }
   public static void main(String[] args)
   {
      demopractical obj = new demopractical();

      obj.display();
   }
}

Post a Comment

1 Comments