Pageviews

Showing posts with label Inheritance. Show all posts
Showing posts with label Inheritance. Show all posts

Thursday, 13 June 2013

Hierarchical Inheritance in Java

by Unknown  |  in Object Oriented Programming at  Thursday, June 13, 2013
Hierarchical Inheritance: This type of Inheritance is a bit complex than the other two types of Inheritance.In this type of Inheritance one class is inherited by many sub classes.It can  also be called as one-to-many relationship.
                                                In hierarchical inheritance,the method of accessing methods of base class and its own class is also different,but that's not a big problem,its difficult for beginners,i too had spent a lot time on understanding this type of Inheritance,so I had decided to present the hierarchical inheritance in the most simple way,below is the diagram and the code for hierarchical inheritance.



WAP to implement Hierarchical Inheritance in Java:


class base{
void display1(){
System.out.println("This is base class method");
}
}
class derived1 extends base                                                     //Derived1 class inherits base class
{
void display2(){
System.out.println("This is derived1 class method");
}
}
class derived2 extends base                                                   //Derived2 class inherits base class
{
void display3(){
System.out.println("This is derived2 class method");
}
}
class Tasic
{
public static void main(String args[])
{
derived1 d1=new derived1();                                                //Derived1 class object declared
d1.display2();                                                                        //Calling its own member
d1.display1();                                                                        //Calling base class member

derived2 d2=new derived2();                                                 //Derived1 class object declared
d2.display3();                                                                        //Calling its own member
d2.display1();                                                                        //Calling base class member
}
}


The output is:

Inheritance


Multilevel Inheritance in Java

by Unknown  |  in Object Oriented Programming at  Thursday, June 13, 2013
Multilevel Inheritance: It is another type of inheritance supported by Java.In Multilevel inheritance, base class is inherited by the derived class,this derived class is further inherited by the other derived class.So,the inheritance goes in levels,that is why it is known as Multilevel inheritance.The diagram shows the criteria;


In the diagram,Class A is base class and Class B and C are derived classes of Class,but Class B acts as a Base class to the Class C,so further if there is any other class for that Class C will act as base class and the process goes on.

Now,we are going to show you an example of how this Inheritance type works.We had created a Simple Calculator which performs basic Arithmetic Operations using Multilevel Inheritance.

WAP to implement the use of Multilevel Inheritance

import java.util.Scanner;
class sport{                                                                           //Base Class declared
int a,b;
void add()
{

Scanner p=new Scanner(System.in);
System.out.println("Enter first number");
a=p.nextInt();
System.out.println("Enter second number");
b=p.nextInt();
int s;
s=a+b;
System.out.println("Sum is"+s);
}
}
class port extends sport{                                                     // Derived class 1 declared
void mul(){
int k;
k=a*b;
System.out.println("Multiplication of 2 nos is:"+k);
}
}
class bot extends port{                                                       // Derived class 2 declared
void sub()
{
int d;
d=a-b;
System.out.println("Subtraction is"+d);
}
}

class pot extends bot{                                                        // Derived class 3 declared
void div()
{
int e;
e=a/b;
System.out.println("Division is"+e);
}
}
class Calculator{
public static void main(String args[])
{
pot p=new pot();
p.add();
p.sub();
p.div();
p.mul();
}
}


Here is the output of the program:

Inheritance









Proudly Powered by Blogger.