World's most popular travel blog for travel bloggers.

Introducing Classes

, , No Comments

 Introducing Class Concept in Java Programming

In the previous article we have learned the basics of class and object related to real life.In this chapter we are going to see the basic programmatic syntax of class.

 Syntax : Class Concept

class classname {
    type instance-variable1;
    type instance-variable2;
    // ...
    type instance-variableN;

    type methodname1(parameter-list) {
      // body of method
    }
    type methodname2(parameter-list) {
      // body of method
    }
    // ...
    type methodnameN(parameter-list) {
      // body of method
    }
}

 Explanation Of Syntax :

Class name

class classname {
  1. class is Keyword in Java used to create class in java.
  2. classname is Name of the User defined Class.

Class Instance Variable

type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
  1. Instance Variables are Class Variables of the class.
  2. When a number of objects are created for the same class,  the same copy of  instance variable is provided to all.
  3. Instance variables have different value for different objects.
  4. Access Specifiers can be applied to instance variable i.e public,private.
  5. Instance Variable are also called as “Fields

Class Methods

type methodname1(parameter-list) {
      // body of method
    }
  1. Above syntax is of Class Methods.
  2. These methods are equivalent to function in C Programming Language.
  3. Class methods can be declared public or private.
  4. These methods are meant for operating on class data i.e Class Instance Variables.
  5. Methods have return type as well as parameter list. We are going to learn this concept deeply in next few tutorials.

 Live Example : Class concept

public class Rectangle {

    // two fields
    public int breadth;
    public int length;

    // two methods
    public void setLength(int newValue) {
        length = newValue;
    }

    public void setBreadth(int newValue) {
        breadth = newValue;
    }

}

Explanation :

RectangleClass name
lengthInstance Variable
breadthInstance Variable
setLength()Method
setBredth()Method

0 comments:

Post a Comment

Let us know your responses and feedback