Iv. Lesson Proper: Applications Development and Emerging Technologies
Iv. Lesson Proper: Applications Development and Emerging Technologies
Iv. Lesson Proper: Applications Development and Emerging Technologies
What are the basic concepts of Inheritance as feature of OOP and how to use it?
Inheritance can be defined as the process where one class acquires the properties (methods and fields) of
another. With the use of inheritance the information is made manageable in a hierarchical order.
The class which inherits the properties of other is known as subclass (derived class, child class) and the
class whose properties are inherited is known as superclass (base class, parent class).
extends Keyword
extends is the keyword used to inherit the properties of a class. Following is the syntax of extends
keyword.
Syntax
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}
Sample Code
Using extends keyword, the My_Calculation inherits the methods addition() and Subtraction() of
Calculation class.
Example
class Calculation {
int z;
The Superclass reference variable can hold the subclass object, but using that variable you can
access only the members of the superclass, so to access the members of both classes it is
recommended to always create reference variable to the subclass.
If you consider the above program, you can instantiate the class as given below. But using the
superclass reference variable ( cal in this case) you cannot call the method multiplication(), which
belongs to the subclass My_Calculation.
Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
Note − A subclass inherits all the members (fields, methods, and nested classes) from its
superclass. Constructors are not members, so they are not inherited by subclasses, but the
constructor of the superclass can be invoked from the subclass.
The super keyword
The super keyword is similar to this keyword. Following are the scenarios where the super
keyword is used.
It is used to differentiate the members of superclass from the members of subclass, if they
have same names.
It is used to invoke the superclass constructor from subclass.
Differentiating the Members
If a class is inheriting the properties of another class. And if the members of the superclass have
the names same as the sub class, to differentiate these variables we use super keyword as shown below.
super.variable
super.method();
Sample Code
This section provides you a program that demonstrates the usage of the super keyword.
In the given program, you have two classes namely Sub_class and Super_class, both have a
method named display() with different implementations, and a variable named num with different values.
We are invoking display() method of both classes and printing the value of the variable num of both
classes. Here you can observe that we have used super keyword to differentiate the members of
superclass from subclass.
Copy and paste the program in a file with name Sub_class.java.
Example
class Super_class {
int num = 20;
Applications Development and Emerging Technologies Page 4 of 10
What are the basic concepts of Inheritance as feature of OOP and how to use it?
super(values);
Sample Code
The program given in this section demonstrates how to use the super keyword to invoke the
parametrized constructor of the superclass. This program contains a superclass and a subclass,
where the superclass contains a parameterized constructor which accepts a integer value, and we
used the super keyword to invoke the parameterized constructor of the superclass.
Copy and paste the following program in a file with the name Subclass.java
Example
class Superclass {
int age;
Superclass(int age) {
this.age = age;
}
public void getAge() {
System.out.println("The value of the variable named age in super class is: " +age);
}
}
public class Subclass extends Superclass {
Subclass(int age) {
super(age);
}
public static void main(String args[]) {
Subclass s = new Subclass(24);
s.getAge();
}
}
Compile and execute the above code using the following syntax.
javac Subclass
java Subclass
On executing the program, you will get the following result −
Output
The value of the variable named age in super class is: 24
IS-A Relationship
IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is
used to achieve inheritance.
public class Animal {
}
public class Mammal extends Animal {
}
public class Reptile extends Animal {
Applications Development and Emerging Technologies Page 6 of 10
What are the basic concepts of Inheritance as feature of OOP and how to use it?
}
public class Dog extends Mammal {
}
Now, based on the above example, in Object-Oriented terms, the following are true −
true
true
true
Since we have a good understanding of the extends keyword, let us look into how
the implements keyword is used to get the IS-A relationship.
Generally, the implements keyword is used with classes to inherit the properties of an interface.
Interfaces can never be extended by a class.
Example
public interface Animal {
}
public class Mammal implements Animal {
}
public class Dog extends Mammal {
}
Example
public class Vehicle{}
public class Speed{}
A very important fact to remember is that Java does not support multiple inheritance.
EXERCISE 1
A student is to create a simple Java program about student enrolment that will classify regular and
irregular students. The parent class is public class Enrolment. If the public class Enrolna is a subclass,
what will be it’s complete Class heading?
EXERCISE 2
Your school would like to create simple enrolment system to help students enrolled in the college.
Create a complete program for public class Enrolment and public class Enrolna extends Enrolment.