Thursday, 28 August 2014

Inheritance, Interfaces, Abstract Classes and Access Specifiers!

//Program for Single_Inheritance
import java.io.*;
class Student
{
    private int roll_no;
    private String name;

    void get_data(int roll_no,String name)
    {
        this.roll_no = roll_no;
        this.name = name;
    }

    void display()
    {
        System.out.print("\nThe Student Details are:");
        System.out.print("\nRoll no: "+roll_no);
        System.out.print("\nName: "+name);
    }
}


class Marks extends Student
{
    private int mark1,mark2;

    Marks(int mark1,int mark2)
    {
        this.mark1 = mark1;
        this.mark2 = mark2;
    }

    void display_marks()
    {
        System.out.print("\nMarks1: "+mark1);
        System.out.print("\nMarks2: "+mark2);

          float avg;
          avg = (mark1+mark2)/2;
          System.out.println("\nAverage: "+avg);
    }
}

public class Single_Inheritance
{
    public static void main(String s[]) throws Exception
    {
          int roll_no,mark1,mark2;
          String name;
         
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         
          System.out.print("\nEnter Roll_no: ");
          roll_no = Integer.parseInt(br.readLine());

          System.out.print("\nEnter Name: ");
          name = br.readLine();

          System.out.print("\nEnter Marks1: ");
          mark1 = Integer.parseInt(br.readLine());

          System.out.print("\nEnter Marks2: ");
          mark2 = Integer.parseInt(br.readLine());

          Marks m = new Marks(mark1,mark2);
          m.get_data(roll_no,name);

          m.display();
          m.display_marks();
    }
}
Output



//Program for Multilevel Inheritance
import java.io.*;
class A
{
     private int a;
    
     void get_a(int a)
     {
        this.a = a;
     }

     void display_a()
     {
        System.out.print("\nA: "+a);
     }
}

class B extends A
{
     private int b;
    
     void get_b(int b)
     {
        this.b = b;
     }

     void display_b()
     {
        System.out.print("\nB: "+b);
     }
}

class C extends B
{
     private int c;
    
     void get_c(int c)
     {
        this.c = c;
     }

     void display_c()
     {
        System.out.println("\nC: "+c);
     }
}

public class Multilevel_inheritance
{
     public static void main(String s[]) throws Exception
     {
          int a,b,c;
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         
          System.out.print("\nEnter a: ");
          a = Integer.parseInt(br.readLine());

          System.out.print("\nEnter b: ");
          b = Integer.parseInt(br.readLine());

          System.out.print("\nEnter c:");
          c = Integer.parseInt(br.readLine());

          C abc=new C();

          abc.get_a(a);
          abc.get_b(b);
          abc.get_c(c);

          abc.display_a();
          abc.display_b();
          abc.display_c();
     }
}
Output


//Program for Abstract class
abstract class A
{
     abstract void a();
     abstract void b();
     void terminate()
     {
        System.out.print("\nTerminating.....");
     }
}

class B extends A
{
     void a()
     {
        System.out.print("\nStarting..... A");
     }
     void b()
     {
        System.out.print("\nStarting..... B");
     }
}

class Abstract_Demo
{
     public static void main(String s[]) throws Exception
     {
          B b1=new B();
          b1.a();
          b1.terminate();
          b1.b();
          b1.terminate();
          System.out.println();
     }
}
Output



//Program for Interface
interface A
{
    void get_A(int a);
}

interface B
{
    void get_B(int b);
}

interface C
{
    void get_C(int c);
}

class D implements A,B,C
{
     int a1,b1,c1;
     public void get_A(int a)
     {
        a1 = a;
     }
     public void get_B(int b)
     {
        b1 = b;
     }
     public void get_C(int c)
     {
        c1 = c;
     }
     void display()
     {
          System.out.print("\nA: "+a1);
          System.out.print("\nB: "+b1);
          System.out.println("\nC: "+c1);
     }
}

class InterfaceDemo
{
     public static void main(String soham[]) throws Exception
     {
          D d = new D();
          d.get_A(10);
          d.get_B(20);
          d.get_C(30);
          d.display();
     }
}

1 comment: