Program to implement the Multiple Inheritance (Exam Interface, Student & Result classes)

Interface looks like class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body). Also, the variables declared in an interface are public, static & final by default. The interface in java is a mechanism to achieve fully abstraction.

The class that implements interface must implement all the methods of that interface. Also, java programming language does not support multiple inheritance, using interfaces we can achieve this as a class can implement more than one interfaces. It cannot be instantiated just like abstract class. To achieve multiple inheritance in Java you have to use interface. The syntax for declaring interface is given below:
interface <interface-name> {
    // variable declarations
 // Method declarations (no implementation)
}

The implements keyword is used to for implementing interface and extends keyword is used to extend the class. The syntax for extending class and implementing interface is:
class <subclass-name> extends <superclass-name> implements <interface-name> {

 // Body of subclass
}












In this program, we have achieved multiple inheritance using interface. We have created an interface Exam which has one method Percent_cal( ) without implementation (body). Then we declare the class Student having data members name, roll_no, marks1, marks2 and method show( ). In class Result we have extended Student class and implemented interface Exam interface as shown in program.


PROGRAM
/* Program to implement the Multiple Inheritance */

interface Exam {
 
 void Percent_cal();
}

class Student {
 
 String name;
 int roll_no, Marks1, Marks2;
 Student(String n, int rn, int m1, int m2) {
  
  name = n;
  roll_no = rn;
  Marks1 = m1;
  Marks2 = m2;
 }
 
 void show() {
  
  System.out.println("Student Name : "+name);
  System.out.println("Roll no : "+roll_no);
  System.out.println("Marks1 : "+Marks1);
  System.out.println("Marks2 : "+Marks2);
 }
}

class Result extends Student implements Exam {
 
 float per;
 Result(String n,int rn,int m1,int m2) {
  
  super(n,rn,m1,m2);
 }
 
 public void Percent_cal() {
  
  int tot = Marks1 + Marks2;
  per = (float)tot / 2;
 }
 
 void display() {
  
  show();
  System.out.println("Percentage = "+per);
 } 
 
}

public class StudentDetails {
 
 public static void main (String[] args) {
  
  Result r = new Result("Aashish",11,75,95);
  r.Percent_cal();
  r.display();
 }
}
OUTPUT
C:\>javac StudentDetails.java
C:\>java StudentDetails
Student Name : Aashish
Roll no : 11
Marks1 : 75
Marks2 : 95
Percentage = 85.0

Comments

  1. Write a program to display student CMS, marks in three subjects and total marks. Consider class student stores the roll no, class test, stores marks of three subjects class result contains total marks obtained in test. The class result can inherit the details of the
    mark obtained in test and roll no of a student through multilevel inheritance.

    ReplyDelete

Post a Comment

Popular posts from this blog

Develop an Android application to display “Hello World!” on screen

Program to design an applet which draws a circle (having color BLUE) inside a triangle (having color YELLOW)

Build a Simple Android App to Display Student Details