Sunday, April 1, 2012

Inheritance


Inheritance
===========
Deriving a new class from an old one is called inheritance.  The old class is known as the base class or super class or parent class and new one is called the subclass or derived class or child class.
The inheritance allows subclasses to inherit all variables and methods of their parent classes.
1. Single inheritance(Only one super class)
2. Multilevel inheritance(Derived from a derived class)
3.  Multiple inheritance(Several super classes)
4.  Hierarchical inheritance(one super class, many subclasses)
5.  Hybrid inheritance
Java does not directly implement multiple inheritance.  However, this concept is implemented using a secondary inheritance path in the form of interfaces.


Defining a Subclass
==================
A subclass is defined as follows:
class subclassname extends superclassname
{
          variable declaration;
          method declaration;
}
The keyword extends signifies that the properties of the superclassname are extended to the subclassname.  The subclass will now contain its own variables and methods as well those of the superclass.

single level inheritance

Program1

import java.io.*;
class Emp{
          String name;
          int no;
          void set(){
                   name="Siva";
                   no=123;
          }
}
class salary extends Emp
{
          int s;
          void setdata(){
                   s=12500;
          }
          void show(){
                   System.out.println(name+"     "+no+"        "+s);
          }
}
class Data{
          public static void main(String arg[])
          {
                   salary s1=new salary();
                   s1.set();
                   s1.setdata();
                   s1.show();
          }
}

Program2
class Room{
          int length,breadth;
          Room(int x,int y)
          {
                   length=x;
                   breadth=y;
          }
          int area()
          {
                   return (length*breadth);
          }
}
class BROOM extends Room{
          int height;
          BROOM(int x,int y,int z)
          {
                   super(x,y);
                   height=z;
          }
          int volume()
          {
                   return(length*breadth*height);
          }
}
class InherTest
{
          public static void main(String args[])
          {
                   BROOM r1=new BROOM(14,12,10);
                   int area1=r1.area();
                   int volume1=r1.volume();
                   System.out.println("Area="+area1);
                   System.out.println("Volume="+volume1);
          }
}



Subclass Constructor
===================
The subclass constructor uses the keyword super to invoke the constructor method of the superclass.
1)      super may only be used within a subclass constructor method.
2)  The call to superclass constructor must appear as the first statement within the subclass constructor.
3)  The parameters in the super call must match the order and type of the instance variable declared in the superclass.

class product
{
          String name;
          int price;
          product(String n,int p)
          {
                   name=n;
                   price=p;
          }
}
class details extends product
{
          String company;
          details(String x,int y,String z)
          {
                   super(x,y);
                   company=z;
          }
          void show(){
                   System.out.println(name+"     "+company+"       "+price);
          }
}
class Data1{
          public static void main(String args[])
          {
                   details d1=new details("Television",8400,"TCL");
                   d1.show();
          }
}
variable access
-------------------
class no
{
          int i;
}
class data extends no
{
          int i;
data(int x,int y)
{
          super.i=x;
          i=y;
}
void show()
{
          System.out.println("Sum="+(super.i+i));
}
}
class Data2
{
          public static void main(String args[])
          {
                   data d1=new data(10,20);
                   d1.show();
          }
}

Multilevel
-------------
import java.io.*;
class Student
{
          String name,dept;
          void set()throws IOException
          {
                   DataInputStream din=new DataInputStream(System.in);
                   System.out.println("Enter name and department:");
                    name=din.readLine();
                   dept=din.readLine();
          }
          void show(){
                   System.out.println("Student name is:"+name+"\nStudent Department="+dept);
          }
}
class curri extends Student
{
          int m1,m2,tot;
          void setMarks()throws IOException
          {
                   DataInputStream din=new DataInputStream(System.in);
                   System.out.println("Enter student mark1:");
                   m1=Integer.parseInt(din.readLine());
                   m2=Integer.parseInt(din.readLine());
                   tot=m1+m2;
          }
          void show1(){
                   System.out.println("Mark1="+m1+"\nMark2="+m2+"\nTotal="+tot);
          }
}
class curriext extends curri
{
          int hei,wei;
          void setpt()throws IOException
          {
                   System.out.println("Enter student height and weight :");
                   DataInputStream din=new DataInputStream(System.in);
                   hei=Integer.parseInt(din.readLine());
                   wei=Integer.parseInt(din.readLine());
          }
          void show2()
          {
                   System.out.println("Height="+hei+"\nWeight="+wei);
          }
}
class multiStudent
{
          public static void main(String args[])throws IOException
          {
          curriext s1=new curriext();
          s1.set();
          s1.setMarks();
          s1.setpt();
          s1.show();
          s1.show1();
          s1.show2();
}
}

Hierarchial
---------------
import java.io.*;
class Student
{
          String name;
          int rno;
          void get()throws IOException
          {
                   DataInputStream din=new DataInputStream(System.in);
                   System.out.println("Enter name and roll number :");
                   name=din.readLine();
                   rno=Integer.parseInt(din.readLine());
          }
}
class college extends Student
{
          String dept;
          double per;
          void getstud()throws IOException
          {
                   DataInputStream din=new DataInputStream(System.in);
                   System.out.println("Enter Department and percentage:");
                   dept=din.readLine();
                   per=Integer.parseInt(din.readLine());
          }
          void show()
          {
                   System.out.println(name+"     "+rno+"      "+dept+"     "+per);
          }
}
class school extends Student
{
          String std;
          double m;
          void getsch()throws IOException
          {
                   DataInputStream din=new DataInputStream(System.in);
                   System.out.println("Enter standard and total mark");
                   std=din.readLine();
                   m=Integer.parseInt(din.readLine());
          }
void disp()
{
          System.out.println(name+"     "+rno+"      "+std+"       "+m);
}
}
class HierStudent
{
          public static void main(String args[])throws IOException
          {
                   college c=new college();
                   school s=new school();
                   System.out.println("Enter details for college student:");
                   c.get();
                   c.getstud();
                   System.out.println("Enter details for School student");
                   s.get();
                   s.getsch();
                   c.show();
                   s.disp();
}
}

Overriding Methods  (Overriden)
=========================
Defining a method in the subclass that has the same name, same arguments and same return type as a method in the superclass.  Then, when that methods is called, the method  defined in the subclass is invoked and executed instead of the one in the superclass.  This is known as overriding.

class Base
{
          int x;
          Base(int a)
          {
                   x=a;
          }
          void display()
          {
                   System.out.println("Base x="+x);
          }
}
class Child extends Base
{
          int y;
          Child(int a,int b)
          {
                   super(a);
                   y=b;
          }
          void display()
          {
                   System.out.println("Base x="+x);
                   System.out.println("Child y="+y);
          }
}
class OverrideTest
{
          public static void main(String args[])
          {
                   Child c2=new Child(100,200);
                   c2.display();
          }
}

The method display() defined in the subclass is invoked.

Final Variables and Methods
======================
All methods and variables can be overridden by default in subclass.  If we wish to prevent the subclass from overriding the members of the superclass,  we can declare them as final using the keyword final as a modifier.

ex:
          final int Size=100;
          final void showStatus(){......}
Final Classes
--------------
          To prevent a class being further subclasses for security reasons.  A class that cannot be subclassed is called a final class.
          ex:
          final class Aclass{...............}
          final class Bclass extends Someclass{...............}

Abstract Methods and Classes
=======================
We can indicate that a method must always be redefined in a subclass, thus making overriding compulsory.  This is done using the modifier keyword abstract in the method definition.
ex:
          abstract class Shape
          {
                   .......................
                   .......................
                   ........................
                   abstract void draw();
                   .........................
                   .........................
          }
          When a class contains one or more abstract methods, it should also be declared abstract.
          Conditions:
          --------------
          1.  We cannot use abstract classes to instantiate objects directly.
          ex:
                   Shape s=new Shape();
                   is illegal because shape is an abstract class.
          2.       The abstract methods of an abstract class must be defined in its subclass.

          3.       We cannot declare abstract constructors or abstract static methods.
ex:
import java.io.*;
abstract class Shapes
{
          abstract void draw();
}
class Design extends Shapes
{
          void draw()
          {
                   System.out.println("Rectangle");
          }
}
class AbsDemo
{
          public static void main(String args[])throws IOException
          {
                   Design d=new Design();
                   d.draw();
          }
}


No comments:

Post a Comment