Chitika

OOP Concepts

Classes Interfaces and Packages


Declaring a class

The syntax for declaring classes in Java is as follows:
class identifier
{
    classBody
}

An Example:

class ExampleClass
  {
   char     cc;
   int         ii;
   double dd;
 void exampleMethod1()
 {
   System.out.println(“Hello World”);
 }
 void exampleMethod2()
  {
   System.out.println(“Hello NIBM”);
  }
 }

The new Operator
In Java Objects are created using the new operator as shown below

               Student s;
               s=new Student();

                          OR

               Student s = new Student();

Invoking Methods in a class

In Java class methods are invoked using class objects as shown below

           Student s = new Student();
           s.register();
           s.getInfo();


Example Java Program

public class Test
    {
    public static void main(String arg[])
        {
        Student s = new Student();
        s.register();
        s.getInfo();
        }
    }

class Student
    {
    char ch;
    int age;
    double dd;
void register( )
         {
         System.out.println("Inside method register");
         }
void getInfo( )
         {
         System.out.println("Inside method getInfo");
         }
    }

On compiling and running this program will print an output as shown below;

           Inside method register
           Inside method getInfo

Constructors

Constructors are special methods you can implement in your classes.

Constructors allow you to perform initialization of member variables and perform any other operations you want to perform (like allocating memory blocks, opening files etc.) when an object is created from a class.

Constructors are always given the same name as that of the class. 

Constructors do not have a return type.

An Example Program

public class Test
    {
    public static void main(String argv[])
        {
        Student s1 = new Student();
        Student s2 = new Student('x',19,200.8);
        System.out.println("s1.cc="+ s1.cc);
        System.out.println("s1.age="+ s1.age);
        System.out.println("s1.dd="+ s1.dd);
        System.out.println("s2.cc="+ s2.cc);
        System.out.println("s2.age="+ s2.age);
        System.out.println("s2.dd="+ s2.dd);
        }
    }
class Student
       {
       char cc;
       int age;
       double dd;
       Student() //beginning of the no-argument constructor
            {
            cc = 'a';
            age = 10;
            dd = 100.4;
            } //end of the constructor
Student(char c,int i, double d) // beginning of the 3 arguments constructor
        {
        cc=c;
        age=i;
        dd=d;
        } //end of the 3 arguments constructor

void register()
      {
      System.out.println("Inside register");
      }
void getInfo()
      {
      System.out.println("Inside getInfo");
      }
}

The above program will print an output as shown below.

       s1.cc=a
       s1.age=10
       s1.dd=100.4
       s2.cc=x
       s2.age=19
       s2.dd=200.8

Deriving Classes

A class can be built on another class that is already defined and is existing.  This already existing class is called the base class or the parent class.  The newly built class is called the derived class or child class.  The child class inherits all the variables and methods of the parent class.  In addition, the child class can have its own variables and methods.The extends keyword is used to derive a child class from the parent class

The syntax for deriving a class from another class is as follows:

	class Car extends Vehicle //child is Car and parent is Vehicle
		{
		// body of the child class (Car)
		}

Class Inheritance

An Example Program

public class Parent
    {
    public static void main(String argv[])
        {
        Child c = new Child();
        System.out.println("c.pi="+ c.pi);  // inherited from parent
        System.out.println("c.ci="+ c.ci);
        c.parentMethod();                    // inherited from parent
        c.childMethod();
        }
    }
class Child extends Parent
      {
       int ci;
       Child()
           {
           ci=100;
           }

     void childMethod()
           {
           System.out.println("Hello Child”);
           }
    }
class Parent
      {
      int pi;
      Parent()
           {
            pi=10;
           }
     void parentMethod()
           {
           System.out.println("Hello Parent");
           }
    }

The above program will print the following output.

         ch.pi=10
         ch.ci=100
         Hello Parent
         Hello Child