Friday 23 November 2012

INHERITANCE

Q.What is inheritance?
    Deriving new sessions from current classification such that the new sessions acquire all the feature of current sessions is known as monetary gift.
     The main advantages of monetary gift is to produce the usability.by using the expands keyword and key phrase we can achieve the inheritance

INHERITANCE:
          Inheritance is a concept where new trainings can be produced from current sessions. the newly designed category acquires all the feature of current category from current category from where it is derived.
Let us take an example to understand the monetary gift concept. A programmer in a software team has written the instructor category as shown here:
class instructor       //teacher class
{
    int id;
   Sequence name;
   string address;
   flow sal;
   //settet technique to shop id
   gap setId(int id)
   {
          this.id = id;
    }
   //getter technique to recover id  
   int getid()
   {
      come back id;
    }
   //to shop name
   gap setname(string name)
  {
     this.name=name ;
   }
   //to recover name
   string getname()
   {
     come back name;
    }
    //to shop address
    gap setaddress(string address)
    {
          this.address=address;
     }
    //to recover address
    string getaddress()
    {
         come back address;
    }
     // to shop salary
     gap setsal (float sal)
    {
      this. sal=sal;
      ]
     //to recover salary
     flow getsal()
    {
        retun sal;
     }
}

       In the previous value observe that all the setter () techniques are mutator method and the gettor () techniques are accessor techniques. Save the previous value as Teacher. Coffee and gather it to get Teacher. Class. These files are then copied into a central data source that is available to every developer in the project team. So other programmers also know that Teacher. Class is available in the data source.
      now another developer wants to develop a undergraduate category as part of he project.
      The undergraduate category contains 75% similarities of the instructor category so while creating the undergraduate category if the developer has thought of recycling instructor category value creating undergraduate category would have been very easy with this idea let us rewrite undergraduate category again. Whatever value is available in instructor category will be omitted in writing the undergraduate category as that value will be automatically available to undergraduate category. For this purpose, simply use the keyword and key phrase  EXTENDS as:
        category Student expands teacher
       The previous statement means all the members of instructor category are available to undergraduate category without rewriting them in undergraduate category. Only additional member should be written in undergraduate category so creating the undergraduate category will become easy as shown in system 2.

PROGRAM 2:
    Write a system to use 'extends' keyword and key phrase to make category by recycling instructor category we should write only additional member in undergraduate category which are not available in instructor category.
//student class
class Student expands teacher
{
//since id name deal with are available instructor category we omit
//those instance variable and the corresponding techniques.
int marks;
//to shop marks
void setMarks(int marks)
{
     this.marks=marks;
}
//to recover represents
int getMarks()
{
   come back mark;
}
}

       Save the previous value as undergraduate.java and gather it to get undergraduate.class. in other words we can say undergraduate category is designed based on instructor category.
Like this creating new sessions from current sessions is known as monetary gift the original category i.e Teacher category is known as super sessions and the newely designed category i.e undergraduate category is known as subclass


/using undergraduate class
class use
{
     public static gap main(string args[])
      {
           //create an item to undergraduate class
           undergraduate s= new student();
          //store data into object-for this use settr methods
           s.setId(1001);
           s.setName("nagaraju");
           s.setaddress("MIG-12, kukatpally, Hyderabad");
           s.setmarks(950);
 //retrieve data using getter techniques and display
           system.out.println("id="+s.getId());
           system.out.println("Name="+s.getName());
           system.out.println("address="+s.getaddress());
           system.out.println("marks="+s.getMarks()
   }
}


OUTPUT:
C:\>javac use.java
c:\>java use

Id =1001
Name=nagaraju
Address=MIG-12, kukatpally, Hyderabad
Marks=950

NOTE:
  When an item to undergraduate category is intended it contains a copy of instructor category within it. This means thee is a relation between the Teacher category and undergraduate c;lass objects. This is the motive why Teacher category members are available to undergraduate category. Note that we do not make Teacher category item but still a copy of available to undergraduate category object

No comments:

Post a Comment