OOPM JAVA - Create class Student (Roll No, name), class Test inherits Student class.

 Experiment No. 10

Aim: Create class Student (Roll No, name), class Test(Marks1,Marks2) inherits Student class. Create class Result which extends Test and has a method named Calculate which finds total as (Total=Marks1+Marks2) and method which display all the details.

Objective: To make aware of Inheritance and its types. 

Outcome: Students implemented different types of Inheritance.

Theory:

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

The syntax of Java Inheritance

class Subclass-name extends Superclass-name  

{  

   //methods and fields  

}  

The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.

In the terminology of Java, a class which is inherited is called a parent or superclass, and the new class is called child or subclass.

Java Inheritance Example

class Employee{  

 float salary=40000;  

}  

class Programmer extends Employee{  

 int bonus=10000;  

 public static void main(String args[]){  

   Programmer p=new Programmer();  

   System.out.println("Programmer salary is:"+p.salary);  

   System.out.println("Bonus of Programmer is:"+p.bonus);  

}  

}  


Types of inheritance in java

On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.

In java programming, multiple and hybrid inheritance is supported through interface only.


 Types of inheritance in Java


Algorithm:

  1. Start
  2. Create class Student (Roll No, name).
  3. Create class Test(Marks1,Marks2) inherits Student class.
  4. Create class Result which extends Test.
  5. Class Result has a method named Calculate which finds total as (Total=Marks1+Marks2).
  6. Define method which displays all the details.
  7. Stop.


Code : 

import java.lang.*;

import java.io.*; 

interface Test 

{

 void totalmarks();

}

class Student 

{

 String name;

 int roll_no,mark1,mark2;

 Student(String n, int r, int m1, int m2) 

 {

  name=n;

  roll_no=r;

  mark1=m1;

  mark2=m2;

 }

 void display() 

 {

  System.out.println ("Name of Student: "+name);

  System.out.println ("Roll No. of Student: "+roll_no);

  System.out.println ("Marks of Subject 1: "+mark1);

  System.out.println ("Marks of Subject 2: "+mark2);

 }

}

class Result extends Student implements Test 

{

 Result(String n, int r, int m1, int m2) 

 {

  super(n,r,m1,m2);

 } 

 public void totalmarks() 

 {

  int total=(mark1+mark2);

  System.out.println ("Total Marks: "+total);

 }

 void display() 

 {

  super.display();

 }

}

public class MultipleInh 

{

 public static void main(String[] args) 

 {

  Result R = new Result("Ayush Parate",36,85,87);

  R.display();

  R.totalmarks();

 }

}


Output : 

Create class Student (Roll No, name), class Test inherits Student class.


Observations and learning: 

(Students are expected to comment on the output obtained with clear observations and learning for each task/ sub part assigned) 

In this experiment we have learnt about the inheritance and the types of inheritance in java.

We implemented the types of inheritance in our program to get the desired output.


Conclusion: 

In conclusion, this experiment helped us knowing the types of inheritance and their uses.


Question of Curiosity 

1. Why multiple Inheritance is not supported by Java?

Ans : Java supports multiple inheritance through interfaces only. A class can implement any number of interfaces but can extend only one class.Multiple inheritance is not supported because it leads to deadly diamond problem. However, it can be solved but it leads to complex system so multiple inheritance has been dropped by Java founders.

 

2. What is the difference between Polymorphism and Inheritance?

Polymorphism

Inheritance

  1. Ability of an object to behave in multiple ways.


  1. Implementation of polymorphism occurs in method level.

  2. Allows calling methods accordingly at compile time and runtime.

  1. Mechanism of allowing a new class to use properties and methods of a superclass.

  2. Implementation of inheritance occurs in class level.

  3. Provides code reusability.




3. What are different types of Inheritance supported by Java?
Ans : Types of inheritance in Java are Single,Multiple,Multilevel & Hybrid.
 
4. Why Inheritance is used by Java Programmers?
Ans : Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of Object Oriented programming system.

Comments