Java Programming

Topic No. 6 : About Inhertitance

About Inheritance

Inheritance is one of the main feature of object-oriented programming because it enables you to cerate hierarchical classifications. Using inheritance, you can mimic the real life concept of inheritance where generally child has inherited the features of parent. For implementation generally we create one class with properties and attributes and this class can then be inherited by other classes, by adding some more peroperties and attributes. In Java, a class that is inherited in other class is called a superclass. The class whcih inherits is called a subclass. Therefore, a subclass is a specialized version of a superclass where it inherits all of the members defined by the superclass and sometimes has its own, unique elements.

Inheritance Example : Simple and Multilevel
Source Code of main class
package javaapplication1;

public class JavaApplication1 {
   
   public static void main(String[] args) {

        C c = new C();
        c.setI(15);
        c.setJ(20);
        c.setK(30);
      
   }
}               
Source Code of A class
class A {
    int i;

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
        System.out.print("\nValue of I is "+i);
    }
    
}                  
Source Code of B class
class B extends A {
    int j;

    public int getJ() {
        return j;
    }

    public void setJ(int j) {
        this.j = j;
         System.out.print("\nValue of J is "+j);
    }
    
}          
Source Code of C class
class C extends B {
    int  k;

    public int getK() {
        return k;
    }

    public void setK(int k) {
        this.k = k;
        System.out.print("\nValue of K is "+k);
    }
    
}            
Usage of super keyword

There are two usages of super keyword : The first is to call the superclass’ constructor. The second is used to access a member of the superclass that has been hidden by a member of a subclass ,particularly with similar name

Source Code of main class
package javaapplication1;

public class JavaApplication1 {
   
   public static void main(String[] args) {

        C c = new C(10,20,30);
        
        c.display();
      
   }
}               


Source Code of A class
class A {
    int i;

    A(int i)
    {
        this.i = i;
        System.out.print("\n Value of I "+i);
    }
}        
Source Code of B class
class B extends A {
    int j;
 
    B(int i,int j)
    {
        super(i);
        this.j = j;
        System.out.print("\n Value of J "+j);
    }
    void display()
    {
        System.out.print("Display Method of B Class");
    }
    
}     
Source Code of C class
class C extends B {
    int  k;

   C(int i,int j,int k)
    {
        super(i,j);
        this.k = k;
        System.out.print("\n Value of K "+k);
    }
   void display()
    {
        
        super.display();
        System.out.print("Display Method of C Class");
    }
    
}         
Practice Exercise : 1
Source Code of main class
package javaapplication1;

public class JavaApplication1 {
   
   public static void main(String[] args) {

        String a0 = args[0];
        String a1 = args[1];
        int a2 = Integer.parseInt(args[2]);
        int a3 = Integer.parseInt(args[3]);
        int a4 = Integer.parseInt(args[4]);
        Result s1 = new Result(a0,a1,a2,a3,a4);
        int total= s1.calculateTotal();
         float per = s1.calculatePer();
         String grade = s1.calculateGrade();
         
         System.out.print("\nStudent ID : " + s1.StudentID);
         System.out.print("\nStudent Name : " + s1.StudentName);
         System.out.print("\nTotal : " + total);
         System.out.print("\nPercentage : " + per);
         System.out.print("\nGrade : " +  grade);
      
   }
}               


Source Code of Student class
public class Student {
    String StudentID,StudentName;

   
    Student(String StudentID,String StudentName)
    {
        this.StudentID = StudentID;
        this.StudentName = StudentName;
    }
    public String getStudentID() {
        return StudentID;
    }

    public String getStudentName() {
        return StudentName;
    }
    
    
}
Source Code of Subject class
public class Subject extends Student {
    int sub1,sub2,sub3;

    Subject(String StudentID,String StudentName,int sub1,int sub2,int sub3)
    {
       super(StudentID,StudentName);
       this.sub1 = sub1;
       this.sub2 = sub2;
       this.sub3 = sub3;
    }
    public int getSub1() {
        return sub1;
    }

    public int getSub2() {
        return sub2;
    }

    public int getSub3() {
        return sub3;
    }
    
    
}
Source Code of Result class
public class Result extends Subject {
    int total;
    float per;
    String grade;
    Result(String StudentID,String StudentName,int sub1,int sub2,int sub3)
    {
        super(StudentID,StudentName,sub1,sub2,sub3);
    }
    int calculateTotal()
   {
      
       this.total = sub1+sub2+sub3;
       return this.total;
   }
   float calculatePer()
   {
       per = this.total/3;
       return per;
   }
   String calculateGrade()
   {
       if(per>60)
       {
           this.grade = "PASS";
       }
       else
       {
           this.grade = "FAIL";
       }
       return this.grade;
   }

      
}
About Interface

An interface is a one type of abstract class that is used to group related methods with empty bodies. Simple Practical is given as below.

Source Code of main class
package javaapplication1;

public class JavaApplication1 {
   
   public static void main(String[] args) {

        D d = new D();
        d.displayA();
        d.displayB();
        d.displayC();
        d.displayD();
        E e = new E();
        e.displayB();
        e.displayC();
      
   }
}               
Source Code of A class
public class A {
    
    public void displayA()
    {
        System.out.print("A-");
    }
}      
Source Code of B interface
public interface B {
    public void displayB();
    
}     
Source Code of C interface
public interface C {
    public void displayC();
}
Source Code of D class
public class D extends A implements B,C {
    
    @Override 
    public void displayB()
    {
        System.out.print("B-");
    }
    @Override 
    public void displayC()
    {
        System.out.print("C-");
    }
    public void displayD()
    {
        System.out.print("D-");
    }
}
Source Code of E class
public class E implements B,C {
    
    @Override 
    public void displayB()
    {
        System.out.print("E1-");
    }
    @Override 
    public void displayC()
    {
        System.out.print("E2-");
    }
    
}
Practice Exercise : 2
Source Code of main class
package javaapplication1;

public class JavaApplication1 {
   
   public static void main(String[] args) {

        String a0 = args[0];
        String a1 = args[1];
        int a2 = Integer.parseInt(args[2]);
        int a3 = Integer.parseInt(args[3]);
        int a4 = Integer.parseInt(args[4]);
        Grade s1 = new Grade();
        s1.getStudentInfo(a0, a1);
        s1.getMarks(a2,a3,a4);
        int total= s1.calculateTotal();
         float per = s1.calculatePer();
         String grade = s1.calculateGrade();
         
         System.out.print("\nStudent ID : " + s1.StudentID);
         System.out.print("\nStudent Name : " + s1.StudentName);
         System.out.print("\nTotal : " + total);
         System.out.print("\nPercentage : " + per);
         System.out.print("\nGrade : " +  grade);
      
   }
}               


Source Code of Student class
public class Student {    
    String StudentID,StudentName;    
   
    public void getStudentInfo(String StudentID,String StudentName)
    {
        this.StudentID = StudentID;
        this.StudentName = StudentName;
    }
    
}
Source Code of Subject interface
public interface Subject {
    public void getMarks(int sub1,int sub2,int sub3);
 }
Source Code of Result interface
public interface Result {
    public int calculateTotal();
    public float calculatePer();
}
Source Code of Grade Class
public class Grade extends Student  implements Subject,Result{
   
   int sub1,sub2,sub3,total;
   float per;
   String grade;
   
   @Override
    public void getMarks(int sub1,int sub2,int sub3)
    {
        this.sub1=sub1;
        this.sub2=sub2;
        this.sub3 = sub3;
    }
    @Override
    public int calculateTotal()
   {
     
      this.total = sub1+sub2+sub3;
      return this.total;
    }
    @Override
    public float calculatePer()
   {
      per = this.total/3;
      return per;
   }
   String calculateGrade()
  {
      if(per>60)
      {
          this.grade = "PASS";
      }
      else
      {
          this.grade = "FAIL";
      }
      return this.grade;
  }
   
}