Object-oriented programming for Java - 1

Java Inheritance


1. Java Inheritance

This OOP method allowed us to create a new class on current class, which named subclass.

The "extends" keyword is used to perform inheritance in Java. For example:

 

2. Method overriding in Java Inheritance

In this method, the subclass will overrides the method in superclass. For example:

In this example, I use override to rebuild the course() function in the second class, after running this program, only the second course() function will be called.

 

3. Super Keyword in Inheritance

In this method, use super keyword will be call the function present in the superclass. For example:

In this example, use super to call the course() function in the University class in the second course() function. After running, the program will be calling the first course function and printout.

4. Protected Members in Inheritance

In this method, use protected keyword can from subclass visit these fields and methods. For example: