Inheritance in JAVA

Inheritance in JAVA

In this tutorial you will learn 1) What is inheritance in JAVA? 2) How Inheritance is useful? 3) What is super class and subclass? 4) Function Overriding 5) How we implement inheritance in JAVA? What is inheritance in JAVA Inheritance is such a mechanism in JAVA by which the methods and data members of one class are inherited form to another class. It must be kept in mind that inheritance is between the two classes which have a lot of features in common. Inheritance is ‘is a’ relationship between two classes. It helps us save code space and the structure of the code is hence improved. How inheritance is useful? Inheritance is a core feature of object oriented programming which allows us to inherit the characteristics of one class to another so it saves us time and code space by allowing us to prevent re writing the code again and again, improving the visibility and debugging also gets easier. We can also override the functions of the superclass in the subclass. The inheritance allows us more flexibility. What is super class and subclass? The base class in C programming is termed as the super class in JAVA programming, similarly the class derived from the super class is called the subclass, with appropriate access specifiers in the super class the methods and data members of the super class can easily be inherited to the subclass so we can say it like subclass is a super class with some additional features. A single subclass can only be inherited by a single superclass, JAVA does not support multiple inheritance as it does not allow a single class to be derived from multiple classes. Function Overriding As the methods from the super class are inherited to the sub class, so if we add some additional functionality to the inherited method in the subclass then this additional change is referred to as function over riding.
  1. //superclass
  2. public class Person{
  3. private String name;
  4. public Person ( ) {
  5. name = “no_name_yet”;
  6. }
  7. public Person ( String initialName ) {
  8. name = initialName;
  9. }
  10. public String getName ( ) {
  11. return name;
  12. }
  13. public void setName ( String newName ) {
  14. name = newName;
  15. }
  16. //subclass
  17. public class Student extends Person {
  18. private int studentNumber;
  19. public Student ( ) {
  20. studentNumber = 0;
  21. }
  22. public Student (String initialName, int initialStudentNumber) {
  23. super(initialName);
  24. studentNumber = initialStudentNumber;
  25. }
  26. public int getStudentNumber ( ) {
  27. return studentNumber;
  28. }
  29. public void setStudentNumber (int newStudentNumber ) {
  30. studentNumber = newStudentNumber;
  31. }
  32.  
  33. }
In this code you can clearly see that in the constructor of the extended class the constructor of the super class has been called. So the changes are made by subclass according to its requirement. How we implement inheritance in JAVA? First we define a super class include all its methods and data members. Then we derive another class from the main class using the key word ‘extends ’ as demonstrated below.
  1. modifier(s) class ClassName extends ExistingClassName modifier(s)
  2. {
  3. memberList
  4. }
  5.  
  6. //Example:
  7. public class Circle extends Shape
  8. {
  9. .
  10. .
  11. .
  12. }
Another example is:
  1. public class Person
  2. {
  3. //body of the class
  4. }
  5. //inheriting here
  6. public class Employee extends Person
  7. {
  8. //class body
  9. }
Super class method can also be called in sub class as you can see in example:
  1. super.MethodName(parameter list);
  2. Or
  3. super(parameter list); //in case of ctor

Comments

Submitted byAnonymous (not verified)on Sun, 03/14/2021 - 17:21

import java.util.Scanner; public class Calculator { public static void main(String[] args) { int num1,num2; char choice; System.out.print("Enter the first number "); Scanner inputScanner= new Scanner(System.in); num1 = inputScanner.nextInt(); System.out.print("Enter the operation to be performed "); choice = inputScanner.next().charAt(0);//1 for addition System.out.print("Enter the second number"); num2 = inputScanner.nextInt(); if(choice =='+'){ System.out.println(num1+num2); //add } else if(choice=='-'){ System.out.println(num1-num2); //subtract } else if(choice=='*'){ System.out.println(num1*num2); //multiply } else if(choice=='/'){ System.out.println(num1/num2); //divide } } }

Add new comment