Monday, 28 November 2016

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

22 comments:

  1. Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article.
    Dissertation Examples

    ReplyDelete
  2. very informative one and i really impressded. best job training in chennai

    ReplyDelete
  3. very interesting article and informative one. i can learn more about java. best java training in chennai

    ReplyDelete
  4. If you want to learn advance Java programming so connect with BR Brains Java training institute in Jaipur, which provide best advance java training course.

    ReplyDelete
  5. a large number of the present understudies getting through the lawyer's exam are experiencing issues following the call of a paper question; they are not recognizing an UNIVERSITY IN KENYA
    adequate number of issues in the reality designs; they are experiencing difficulty applying the law to the certainties and breaking down them; and their punctuation and spelling isn't satisfactory.

    ReplyDelete
  6. Today, most clients have able customer machines which can do considerably more than HTML permits. what is programming language

    ReplyDelete
  7. Having Insufficient Material: Nothing aggravates a book advertiser more than endeavoring to advance a book that isn't promotable. What makes a book unfit to be advanced? amryl

    ReplyDelete
  8. high-quality web site, where did u come happening taking into consideration the mention coarsely this posting?i've addiction in a number of the articles almost about the order of your website now, and i in reality in imitation of your fashion. thanks 1,000,000 and engross keep going on the functioning discharge loyalty. BA 2nd Year Results

    ReplyDelete
  9. In your blog I was happy to see your article, better than last time, and have made great progress, good samaritan school jasola I am very pleased. I am looking forward to your article will become better and better.

    ReplyDelete
  10. Native apps do not need internet connectivity to function this gives users anytime, anywhere access.
    mobile app development london

    ReplyDelete
  11. Very efficiently written information. BCom 1st Year Result It will be beneficial to anybody who utilizes it, including me. Keep up the good work. BCom 2nd Year Result For sure i will check out more posts. This site seems to get a good amount of visitors.BCom 3rd Year Result

    ReplyDelete
  12. Thank you for this! this is very helpful. I'm impressed with your blo...

    BA 2 YEAR REVALUATION RESULT

    ReplyDelete
  13. This is a great post. BA 1st Year Result I like this topic.This site has lots of advantage. BA 2nd Year Result I found many interesting things from this site. It helps me in many ways.Thanks for posting
    this again. BA 3rd Year Result

    ReplyDelete
  14. This is a great inspiring article.I am pretty much pleased with your work.You can really really helpful information. Keep it up. Keep blogging. Looking to your next post.
    best dissertation writing service

    ReplyDelete
  15. There are lots of dissertation internet websites on-line since you attain web famous from the site. web services testing

    ReplyDelete