Pageviews

Showing posts with label Object Oriented Programming. Show all posts
Showing posts with label Object Oriented Programming. 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









Inheritance in Java

by Unknown  |  in Object Oriented Programming at  Thursday, June 13, 2013
Inheritance is the another powerful feature provided by Object Oriented Programming.It is the process of inheriting features from one class to the other.The class from which features are inherited is known as Base Class/Super Class/Parent class.The class which inherits the features is known as Derived class/Sub Class/Child class.The derived class consists of features of base class as well as its own features.

Most of us gets confused with the types of Inheritance,so here is the simple way to learn the types of Inheritance.In Java,we use 'extends' keyword when a class inherits features of another class.Lets take an example;

class derived extends base  //This means that derived class inherits base class features.

Java supports 3 types of Inheritance,i.e.


  • Single level Inheritance.
  • Multi-level Inheritance.
  • Hierarchical Inheritance


Single level Inheritance:In single level inheritance single Derived class inherits the features of Base class.In this post only single level inheritance is discussed,the other two types  will be explained in the next post.



As the above diagram shows Class A is the base class and it is derived by the Class B.Here is the simple example of a program to show the use of single level inheritance.

WAP to implement Single level Inheritance:

import java.util.*;
class base{                                                                              //Base class declared
void add()
{
Scanner input=new Scanner(System.in);
System.out.println("Enter first number");
int a= input.nextInt();
System.out.println("Enter Second number");
int b= input.nextInt();

int s;
s=a+b;
System.out.println("Your result  is"+s);
}
}
class inherit extends base{                                                         //Derived class inherits base class
public static void main(String args[]){
base b=new base();
b.add();
}
}

The output goes here:

Inheritance






Monday, 10 June 2013

Introduction to arrays in Java

by Unknown  |  in Object Oriented Programming at  Monday, June 10, 2013
Arrays are the most common data structure used in almost all high level programming languages.Arrays are a homogeneous collection of elements i.e. you can declare an array which consists of elements of similar data type only.For e.g. if we declare an array of data type int then we can use elements with int data type only.Array store elements in continuous memory locations.Arrays are of 2 types:


  • Single Dimensional arrays.
  • Multi Dimensional arrays.


Single Dimensional arrays:  In these types of arrays,we can use only single dimensional elements.


Multi Dimensional arrays:In these types of array two or more than two dimensional array can be declared.They are also known as matrix arrays,because they can be stored in the form of matrix.

In the following example we had declared a single dimensional array,which takes input of 5 elements and then displays the entered values.

WAP to show the array implementation in Java:

import java.util.Scanner;
class Array3
{
public static void main(String args[])
{
Scanner kp=new Scanner(System.in);
int play[]=new int[4];                                      //Array Declaration
System.out.println("Enter values of array");
for(int i=0;i<play.length;i++)
{
play[i]=kp.nextInt();                                          // Array Input
}
System.out.println("Entered elements are");
for(int i=0;i<play.length;i++)
{
System.out.print(play[i]);
}}
}


The output goes here:

Arrays

Proudly Powered by Blogger.