Friday, September 2, 2011

Programs on multilevel inheritance in Java


We know that multilevel inheritance means where one class inherits the properties of the object of any pre defined class. The class which inherits is called sub class or some times derived class although the word derived is related to C++ programming language. The class which is being inherited is called super class. Sometimes it is referred as base class. Normally the word base class associated with C++ programming language.

Inheritance means acquiring the properties of any predefined class by a class and it may continue for several layers. For example, suppose ‘A’ is a defined class and class ‘B’ inherits class ‘A’. Again class ‘C’ may inherits class ‘B’ and the process may continue.

Let’s see what is the meaning of ‘acquiring the properties’.

Let’s start with a program.

import java.io.*;
class Record
{
String name[]=new String[10];
String add[]=new String[10];
int i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 public void take()throws Exception
 {
 for(i=0;i< 10;i++)
 {
  System.out.println("Enter name:");
  name[i]=br.readLine();
 System.out.println("Enter address:");
  add[i]=br.readLine();
}
 }
 public void display()
 {
      System.out.println("Name           Address");
      for(i=0;i< 10;i++)
      {
           System.out.println(name[i]+ "  "+add[i]);
        }
    }
   
}
class FinalRecord extends Record
{
String temp;
int i,j;
  public void sort()
  {
      for(i=0;i< 9;i++)
      {
           for(j=i+1;j< 10;j++)
           {
               if(name[i].compareTo(name[j])>0)
               {
                   temp=name[i];
                   name[i]=name[j];
                   name[j]=temp;
                   temp=add[i];
                   add[i]=add[j];
                   add[j]=temp;
                }
            }
        }
    }


  public static void main(String args[]) throws Exception
  {
   Prime ob=new Prime();
  ob.take();
  System.out.println("Entered records are\n");
  ob.display();
  ob.sort();
  System.out.println("Records in sorted order\n");
  ob.display();
   }
   }
  
In the above program, class Record contains two String class arrays to store name and address and two functions. One function will store the records (name and address) and the other function will display the records.
If we create object of the class ‘Record’ then with the object we can only perform the above jobs as defined within function body.

Now, our sub class FinalRecord inherits the class Record. It means that the objects of class FinalRecord can use all the members defined in the above class ‘Record’ and also the members defined within its body. Here one point to be remembered that the sub class of this above program can access all the members of its super class as the members in the super class are not of ‘private’ access specifier.
Sub class ‘FinalRecord’ has defined another function to sort the names in alphabetical order and accordingly the addresses will be arranged.

Inside the main() function, object of sub class ‘FinalRecord’ is created and all the members ( data member and function member) of the super class plus the function ‘sort()’ which is exclusively defined in the sub class present in the object of ‘FinalRecord’ class.

2 comments:

  1. show an null error in if(name[i].compareTo(name[j])>0) line
    please send me correct in my email durgapur97@rediffmail.com

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner