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

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative