Friday 23 November 2012

INHERITANCE CONST..15

PROGRAM 5:
     Create software to confirm that the regular constructor of the extremely category is available  to sub category by standard.
    //calling extremely category normal constructor
    category one
    {
      //super category standard constructor
            one()
             {
               program.out.println("one");
              }
       }
     category two expands one
      {
         // sub category standard constructor
          two()
          {
             program.out.println("two");
           }
    }
   category extremely 1
     {
        community fixed gap main(string args[])
         {
              //create sub category object
               Two t =new two();
          }
    }

OUTPUT:
C:\>javac extremely 1.java
C:\>jaa extremely 1
one
two

      Please notice that when sub category item is designed first of all the extremely category standard constructor is known as and then only the sub category constructor is known as.
In the following program we take a parameterized constructor in the extremely category .this is not available to sub category by standard so it should be known as by using extremely keyword and key phrase.

PROGRAM 6:
      Create software to realize that the parameterized constructor of the extremely category can be from sub known as from sub category using extremely ()
     //calling extremely category parameterized constructor from sub class
     category one
      {
          //super category var
          int i;
          //super category para constructor
          one(int i)
          {
                 this.i =i;
          }
}
class two expands one {
{
    //sub category var
      int i;
     //sub category para constructor
      two(int a. int b)
       {
                 super(a); //call extremely category constructor and complete a.
                 i= b; //initialize sub category var
        }
       //sub category method
        gap show()
        {
              program.out.println("sub category i= "+i);
              program.out.println("super category i= "+ extremely.i);
       }
}
class extremely 1
{
    community fixed gap primary (string args[])
     {
        //create sub category object
        two t = new two(11,22)
       //call sub category method
       t.show();
      }
}  

OUTPUT:
C:\>Java extremely 1.java
c:\>Java extremely 1

Sub category i=22
super category i=11

 
     In the previous program there is a parameterized constructor in incredibly category which initializes the circumstances varying i, as:

one (int i)
 {
      this .i = i,
 }
this constructor can be known as type sub category by composing another parameterized constructor in sub category and moving value to it as:
two(int a, int b)
{
     super(a); //call extremely category constructor and complete a
      i = b; //initialized sub category var
}

The previous constructor will get information when the sub category item is designed as:
   two t = new two(11,22);
     So the 11 and 22 are duplicated into a and b respectively. And then the value 'a' is sent to the uper category parameterized constructor by using declaration :
        super(a);
    one situation is that the declaration contacting the extremely category constructor should be the first one in sub category constructor what this means is that the extremely category constructor should be the given concern over the sub category constructor.

THE PROTECTED SPECIFIER:
     The personal associates of the extremely category are not available to sub sessions straight. But some periods there my be a need to accessibility the information of extremely category. for this objective protected specifier is used protected is widely used in extremely category for making the associates of the extremely category available straight in its sub sessions. We can think that the protected specifier performs like community with regard to sub   sessions. See this technique 7

PROGRAM 7:
  Create software to comprehend personal associates are not available in sub category but protected associates are available in sub class
//private and protected
class Access
{
    personal int a;
    protected int b;
}
class sub expands access
{
    community gap get()
      {
          program.out.println(a);//error - a is private
           program.out.println(b);
        }
}
class Check
  {
   community fixed gap main(string args[])
         {
             sub s= new sub();
                          s.get();
            }
}


OUTPUT:
C:\>javac Check.java

Test.java:11: a has personal accessibility in access
system.out.println(a);

1error

No comments:

Post a Comment