Classes, Objects & Methods

Home   »    Java   »    Classes, Objects & Methods


We know that java is purely OOP language that is all the code of java is written in the forms of classes There is a main Method which also Reside in Class But In java we can also Create Classes for Reusability of Code .All the Code must be Reside in Classes and if any one wants to use any data or member functions from Class then first We have to create object of that class then with the help of dot operator we can call or use any data member or either member function from the class Java allows all OOPS Concepts like Data abstraction ,Inheritance, Polymorphism and Data Encapsulation all these are Achieved only you will Create Code in the Form Classes.

A Class Contains
1) Data Members or Simply Called As Variables
2) Member Functions or Simply Called as Functions

Contents



Defining a Class

a class is a user defined data type used to implement an abstract an abstract object, giving you the capability to use object-oriented programming with java.

Once the class-type has been defined, we can create 'variables' of that type. These variables are termed as instance of classes, which are the actual objects.

The general form of a class definition is

class name [extends superclass]
{
  [Variable declarations;]
  [methods declarations;]
}

Everything written inside the square brackets is optional. Thus, the following would be a valid class definition.

class Empty
{ }

Classname and Superclass name are any valid java identifiers, The keywords extends indicates that the properties of the superclass class are extened to the class classname. This concept is know as inheritance.





Adding Variables

Data is encpsulated in class by placing data fields inside the body of the class definition. These variables are called instance of variables because they are created whenever an object of class is instantiated. Instance variables can be declared exactly the same way as we declare local variables.

Example

class Movie
{
  string title;
  int length;
  int cost;
  char type;
}

The class movie contains four instance variables, out of which one is of type string, two are of type int and one is of type char. The two int variables can be declared in one line as

int length, cost;

Remember these variables are only declared and therefore no storage space has been created in the memory. Instance variables are also known as member variables.





Adding Methods

A class has no life without methods. The objects created by such class cannot respond to any message. Therefore it is necessary to add methods for manipulating the data contained in the class. Methods are declared inside the class immediately after the variable declaration. The general form of a method declaration is

type methodname (parameter-list)
{
  method-body;
}

where, - methodname denotes the name of the method.
- type denotes the type of the value the method returns.
- parameter-list denotes the list of parameters to be passed into the method.

The parameter-list must always be enclosed in paranthesis. The variables in the list are separated by commas. In the case where no input data is required, the declaration must retain the empty paranthesis.

Let us consider the Movie class again and add a method getData() to it.

class Movie
{
  String title;
  int length;
  int cost;
  char type;
  void getData(String s; int x, int y, char z)
  {
    title = s;
    length = x;
    cost = y;
    type = z;
  }
}


Note that the method has a return type void because it does not return any value. The getData() method is basically added to provide values to the instance variables.

Most of the times when we use classes, we will have many methods and variables within the class. Instance variables and methods in classes are accessible by all the methods in the class but a method cannot access the variables declared in other methods. Example:

class varAccess
{
  int x;
  void method1()
  {
    int y;
    x = 10; // legal
    y = x; // legal
  }
  void method2()
  {
    int z;
    x = 5; //legal
    z = 10; //legal
    y = z; //illegal
  }
}





Accessing Class Members

After creating objects, we are discussing about the accessing class members using objects. Each objects containing its own set of variables, we should assign values to these variables in order to use them in our program. Since we are outside the class, we cannot access the instance variables and the methods directly. Thus, to access them, we use the concerned object with the dot operator. The syntax is as follows:

Object_name.Variable_name;
Object_name.Method_name (parameter_list);

Here object_name is the name of the object of class and variable_ae is the name of the instance variable inside the object.
Similarly, method_name is hte method inside the object and parameter_list is the comma-separated list of the actual values that must match in type and number with the parameter list of the methodname declared in the class.
The instance variable and method of Movie class may be accessed and assigned values as follows:

mov1.length = 15;
mov1.cost = 150;
mov1.length = 25;
mov2.cost = 250;

Both objects store different values, and any changes in one have no effect on other.

This is way of assigning values to the variables in the object directly calling by their object using dot operator.

The other and more convenient way of assigning values to the instance variables is to use a method that is declared inside the class.

The Complete Class Program...



Output of "CompleteClassProgram.java"


Download Complete Program

  -  










Constructor

Constructor is that whose name is Same as name of Variable and used for Initializing the data members of Class and it gets Automatically called when an object of class is created But Always Remember Some things about Constructor Like :-

Name of Constructor must be same as name of class
Have No Return Type Even Not Void
Automatically Called when an object of class is created
There are three types of Constructor as follows:-

Default Constructor


Default Constructor is also called as Empty Constructor which has no arguments and It is Automatically called when we creates the object of class but Remember name of Constructor is same as name of class.




Output of "Constructor.java"


Download Complete Program

  -  






Parameterized Constructor


This is Another type Constructor which has some Arguments and same name as class name but it uses some Arguments So For this We have to create object of Class by passing some Arguments at the time of creating object with the name of class.




Out of "ParamConstructor.java"


Download Complete Program

  -  




Copy Constructor


This is also Another type of Constructor. In this Constructor object of another Constructor is passed As name Suggests you Copy means Copy values of another Class object This is used for Copying the values of class object into an another object of class So For Calling Copy Constructor We have to pass the name of object whose values we wants to Copying.




Output of "CopyConstructor.java"


Download Complete Program

  -  




Finalize

We Know that in C++ destructor is used to free up the Memory that is allocate by Constructor Java Doesn't provide Destructor So For Deallocating the Memory that is Allocated by Constructor So For this Finalizer Method is Used For Free up the Memory Space that is allocated by Constructor For Free up Memory we have to use the Finalizer Method and it also Declared as Protected because Finalizer method is reside in Java.lang Package and you must have to override the Definition of the Finalizer SO that it must be define as Protected The Finalize Method is used for Performing the tasks those are always performing at the End.




Output of "FinalizeProgram.java"


Download Complete Program

  -  




Method Overloading

Method Overloading is also Called as Function Overloading. Overloading Means a Functions has many Behaviors occurred When in class when a functions has same name but different behaviors A Functions said to be overloaded When :-

1) Function has same Name but Different Return Type
2) Difference in No of Arguments
3) Different Return Type in Arguments
When We Pass a Call for Execution then it will Match the Criteria of Function Like Number of Arguments and Data types etc




Output of "MethodOverloading.java"


Download Complete Program

  -  





<< Previous Topic
Next Topic >>

4 comments:

  1. I really appreciate you for all the valuable information that you are providing us through your blog.
    Java Training with Placements

    ReplyDelete
  2. However, it is important to note that webtechmantra operates in a legal gray area. The site provides access to copyrighted material without the permission of the copyright owners. Streaming or downloading copyrighted material without permission is illegal, and users could face legal consequences for doing so. read more

    ReplyDelete