Monday 13 April 2015

Inheritance in java

package inhrit;

import java.util.Scanner;

public class inhrit {


public static void main(String[] args) {


abc a=new abc();
        a.func1();
        a.func2();
        def d=new def();
        d.func1();
        d.func2();
        uvw u=new uvw();
        u.func1();
        u.func2();
        mno x=new mno();
        x.func1();
        x.func2();
     }
}
interface pqr
{
 void func1();
 void func2();
}



class abc implements pqr
{

public void func1()
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a");
a=sc.nextInt();
System.out.println("Enter the value of b");
b=sc.nextInt();
c=a+b;
System.out.println("Addition is:"+c);


}

public void func2()
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a");
a=sc.nextInt();
System.out.println("Enter the value of b");
b=sc.nextInt();
c=a*b;
System.out.println("Multiplication is:"+c);

}

}


class def implements pqr
{

public void func1()
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a");
a=sc.nextInt();
System.out.println("Enter the value of b");
b=sc.nextInt();
c=a-b;
System.out.println("Substraction is:"+c);

}

public void func2()
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a");
a=sc.nextInt();
System.out.println("Enter the value of b");
b=sc.nextInt();
c=a/b;
System.out.println("Divide is:"+c);

}

}

class uvw implements pqr
{

public void func1()
{
int row, stars;
   
     for ( row = 1 ; row <= 5 ; row++ )
     {
        for ( stars= 1 ; stars<= row ; stars++ )
        {
           System.out.print("*");
         
        }

        System.out.println();
     }

}

public void func2()
{
int i,j;
        for(i=1;i<=5;i++)
        {
            for(j=0;j<=5-i;j++)
            {
                System.out.print("*");
            }
            System.out.print("\n");
        }

}

}

class mno implements pqr
{

public void func1()
{
int i,j,k;
        for(i=0;i<5;i++)
        {
            for(j=1;j<5-i;j++)
            {
                System.out.print(" ");
            }
            for(k=0;k<=i;k++)
            {
            System.out.print("*");
            }
        System.out.print("\n");
    }

}

public void func2()
{
int i,j,k;
   for(i=0;i<5;i++)
   {
      for(j=0;j<i;j++)
      {
          System.out.print(" ");
      }
      for(k=0;k<5-i;k++)
      {
          System.out.print("*");
      }
      System.out.print("\n");
   }

}

}

No comments:

Post a Comment