Learn the Basics of Classes using Java

A Class is the basic concept in a Object Oriented Programming language. It’s the template for multiple objects with similar features.

Consider the following statements

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">class</span> Family {
  2: String dadname;
  3: String mumname;
  4: String child;
  5: <span style="color: #0000ff">int</span> dogs; } 

Here, a class called family is created. It is treated as an object. A family may be having dad, mum, children, dogs. These are all its attributes or properties. Now let us examine how the variables plays around the classes.

Normally, we declare variables inside the main statement. But variables declared outside the main method are called as Instance Variables in Java. A special reference (Object) is created inside the main method for the instance variables using dot operator and new keyword. A simple example will help in understanding the concept.

An Illustration

The example given below shows the usage of Instance variables. Variables named x and y are declared above the main method. Inside the main method, object of the class is created and variables are accessed using the dot operator

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">class</span> Inst {
  2: <span style="color: #0000ff">int</span> x = 30; <span style="color: #008000">//instance variable declared</span>
  3: <span style="color: #0000ff">int</span> y = 20; <span style="color: #008000">//instance variable declared</span>
  4: Public <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String args []) {
  5: Inst in = <span style="color: #0000ff">new</span> Inst(); 
  6: <span style="color: #008000">//instance of class using new keyword</span>
  7: System.out.println(&quot;<span style="color: #8b0000">The value of x is </span>&quot; +in.x); 
  8: <span style="color: #008000">//displays x's value</span>
  9: System.out.println(&quot;<span style="color: #8b0000">The value of y is </span>&quot; +in.y); 
 10: <span style="color: #008000">//displays y's value</span>
 11: } 
 12: }  

The output of the above code will be:

READ  Introduction to Java

The value of x is 30

The value of y is 20

Methods in Java

Methods are functions that operate on instances of class. We declare as many functions outside the main method (called Instance Method) and they can be called inside the main method by using dot operator and new keyword (creating object of the class). Let us examine one example:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">class</span> Rect {
  2: <span style="color: #0000ff">int</span> length = 30;
  3: <span style="color: #0000ff">int</span> breadth = 30;
  4: <span style="color: #0000ff">void</span> calculate() { <span style="color: #008000">//method to calculate area</span>
  5: <span style="color: #0000ff">int</span> area = length * breadth;
  6: System.out.println(&quot;<span style="color: #8b0000">The Area is </span>&quot; +area + &quot;<span style="color: #8b0000">sq.cms</span>&quot;);
  7: }
  8: <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main (String args[]) {
  9: Rect p = <span style="color: #0000ff">new</span> Rect();
 10: p.calculate();
 11: }
 12: } 

The output will be

The Area is 900 Sq.cms

In the same way we can pass arguments inside the methods. In the above example instead of declaring variables simply write int <variable name> as a parameter for the calculate function, then you should supply a value as a parameter for the calculate function inside the main function. It’s examined below for your reference:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">class</span> Rectparam {
  2: <span style="color: #0000ff">void</span> calculate(<span style="color: #0000ff">int</span> length, <span style="color: #0000ff">int</span> breadth) { <span style="color: #008000">//method to calculate area</span>
  3: <span style="color: #0000ff">int</span> area = length * breadth;
  4: System.out.println( &quot;<span style="color: #8b0000"> The Area is </span>&quot; +area + &quot;<span style="color: #8b0000">sq.cms</span>&quot;);
  5: }
  6: <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main (String args[]) {
  7: Rect p = <span style="color: #0000ff">new</span> Rect ();
  8: p.calculate(30,30);
  9: }
 10: } 

Java supports method overloading .It means the method name will be same but parameters will be different. Each function takes different parameters like string, integer, float etc. Then an instance of class is created inside the main and appropriate values are passed on to it.

READ  Learn how to work with SQL

Following example shows the usage of Method Overloading in Java:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">class</span> Overload
  2: <span style="color: #0000ff">void</span> show(<span style="color: #0000ff">int</span> x) {
  3: System.out.println(&quot;<span style="color: #8b0000">The value is </span>&quot; +x);
  4: }
  5: <span style="color: #0000ff">void</span> show(<span style="color: #0000ff">int</span> a,<span style="color: #0000ff">int</span> b) {
  6: <span style="color: #0000ff">int</span> c = a*b;
  7: System.out.println(&quot;<span style="color: #8b0000">The value is </span>&quot; +c);
  8: }
  9: <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String args[]) {
 10: Overload over = <span style="color: #0000ff">new</span> Overload();
 11: over.show(50);
 12: over.show(60,70);
 13: }
 14: } 

Constructors

It is the special kind of method where the constructor name is the same as its class name and it can also be overloaded just like method overloading. The main purpose of a constructor is to provide a way for new objects to be initializes in a special way. Finalizers are used to dispose an object. It is called before Java’s automatic garbage collector reclaims an unused object.

The method to be used is finalize().

Leave a Reply

Your email address will not be published.