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

Slider

Image Slider By engineerportal.blogspot.in The slide is a linking image  Welcome to Engineer Portal... #htmlcaption

Tamil Short Film Laptaap

Tamil Short Film Laptaap
Laptapp

Labels

About Blogging (1) Advance Data Structure (2) ADVANCED COMPUTER ARCHITECTURE (4) Advanced Database (4) ADVANCED DATABASE TECHNOLOGY (4) ADVANCED JAVA PROGRAMMING (1) ADVANCED OPERATING SYSTEMS (3) ADVANCED OPERATING SYSTEMS LAB (2) Agriculture and Technology (1) Analag and Digital Communication (1) Android (1) Applet (1) ARTIFICIAL INTELLIGENCE (3) aspiration 2020 (3) assignment cse (12) AT (1) AT - key (1) Attacker World (6) Basic Electrical Engineering (1) C (1) C Aptitude (20) C Program (87) C# AND .NET FRAMEWORK (11) C++ (1) Calculator (1) Chemistry (1) Cloud Computing Lab (1) Compiler Design (8) Computer Graphics Lab (31) COMPUTER GRAPHICS LABORATORY (1) COMPUTER GRAPHICS Theory (1) COMPUTER NETWORKS (3) computer organisation and architecture (1) Course Plan (2) Cricket (1) cryptography and network security (3) CS 810 (2) cse syllabus (29) Cyberoam (1) Data Mining Techniques (5) Data structures (3) DATA WAREHOUSING AND DATA MINING (4) DATABASE MANAGEMENT SYSTEMS (8) DBMS Lab (11) Design and Analysis Algorithm CS 41 (1) Design and Management of Computer Networks (2) Development in Transportation (1) Digital Principles and System Design (1) Digital Signal Processing (15) DISCRETE MATHEMATICS (1) dos box (1) Download (1) ebooks (11) electronic circuits and electron devices (1) Embedded Software Development (4) Embedded systems lab (4) Embedded systems theory (1) Engineer Portal (1) ENGINEERING ECONOMICS AND FINANCIAL ACCOUNTING (5) ENGINEERING PHYSICS (1) english lab (7) Entertainment (1) Facebook (2) fact (31) FUNDAMENTALS OF COMPUTING AND PROGRAMMING (3) Gate (3) General (3) gitlab (1) Global warming (1) GRAPH THEORY (1) Grid Computing (11) hacking (4) HIGH SPEED NETWORKS (1) Horizon (1) III year (1) INFORMATION SECURITY (1) Installation (1) INTELLECTUAL PROPERTY RIGHTS (IPR) (1) Internal Test (13) internet programming lab (20) IPL (1) Java (38) java lab (1) Java Programs (28) jdbc (1) jsp (1) KNOWLEDGE MANAGEMENT (1) lab syllabus (4) MATHEMATICS (3) Mechanical Engineering (1) Microprocessor and Microcontroller (1) Microprocessor and Microcontroller lab (11) migration (1) Mini Projects (1) MOBILE AND PERVASIVE COMPUTING (15) MOBILE COMPUTING (1) Multicore Architecute (1) MULTICORE PROGRAMMING (2) Multiprocessor Programming (2) NANOTECHNOLOGY (1) NATURAL LANGUAGE PROCESSING (1) NETWORK PROGRAMMING AND MANAGEMENT (1) NETWORKPROGNMGMNT (1) networks lab (16) News (14) Nova (1) NUMERICAL METHODS (2) Object Oriented Programming (1) ooad lab (6) ooad theory (9) OPEN SOURCE LAB (22) openGL (10) Openstack (1) Operating System CS45 (2) operating systems lab (20) other (4) parallel computing (1) parallel processing (1) PARALLEL PROGRAMMING (1) Parallel Programming Paradigms (4) Perl (1) Placement (3) Placement - Interview Questions (64) PRINCIPLES OF COMMUNICATION (1) PROBABILITY AND QUEUING THEORY (3) PROGRAMMING PARADIGMS (1) Python (3) Question Bank (1) question of the day (8) Question Paper (13) Question Paper and Answer Key (3) Railway Airport and Harbor (1) REAL TIME SYSTEMS (1) RESOURCE MANAGEMENT TECHNIQUES (1) results (3) semester 4 (5) semester 5 (1) Semester 6 (5) SERVICE ORIENTED ARCHITECTURE (1) Skill Test (1) software (1) Software Engineering (4) SOFTWARE TESTING (1) Structural Analysis (1) syllabus (34) SYSTEM SOFTWARE (1) system software lab (2) SYSTEMS MODELING AND SIMULATION (1) Tansat (2) Tansat 2011 (1) Tansat 2013 (1) TCP/IP DESIGN AND IMPLEMENTATION (1) TECHNICAL ENGLISH (7) Technology and National Security (1) Theory of Computation (3) Thought for the Day (1) Timetable (4) tips (4) Topic Notes (7) tot (1) TOTAL QUALITY MANAGEMENT (4) tutorial (8) Ubuntu LTS 12.04 (1) Unit Wise Notes (1) University Question Paper (1) UNIX INTERNALS (1) UNIX Lab (21) USER INTERFACE DESIGN (3) VIDEO TUTORIALS (1) Virtual Instrumentation Lab (1) Visual Programming (2) Web Technology (11) WIRELESS NETWORKS (1)

LinkWithin