TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Tutorials

Inheritance Examples

Overview

In this post, we will learn about Inheritance and its different forms of it with examples. Inheritance is the most important and powerful feature of object-oriented programming. Using inheritance we can create new classes called sub-classes or derived classes from an existing superclass or parent class.

‘is-a’ or ‘is-an’ Relationship

As a rule of thumb, we use inheritance when the classes have an ‘is-a’ relationship. Let’s consider an example to understand the rule. Let’s say we have an Animal superclass and a Cat class. Now, try to affirm that Cat ‘is-a’ or /’is-an’ Animal. True
So, we can go ahead and use inheritance to design the Cat class that extends the Animal class. In Java, we use the keyword “extends” to extend a class from an existing superclass.

‘HAS-A’ or ‘has-an’

Now let’s consider another example.

Consider class Fan and another class Capacitor. Using the same rule try to ask yourself, Capacitor is a Fan?  No

The answer is no this time. So, we may not use the inheritance relationship between the two classes. Even though every fan has a capacitor part inside it. We use the aggregation relationship. Fan ‘has a capacitor part in it.

Single and Multiple Inheritance

When a class is derived or subclassed from one base/super class then it’s called Single Inheritance.

On the other hand, when a sub-class is derived from two or more superclasses then it’s called multiple inheritance. One key point to remember here is that Java doesn’t support multiple inheritance directly. We may need to use interfaces to come across this limitation.

Multi-level Inheritance

When a class is derived from a class that is also derived from a parent class as shown in the picture, this type of inheritance is called multi-level inheritance.

 

 

Sample Java Code

package inheritance;

public class BaseClass {

 public String classInfo() {
 return "BaseClass";
 }

}

Derived Class

package inheritance;

public class DerivedClass extends BaseClass {

 @Override
 public String classInfo() {
 return "DerivedClass";
 }

 public void extraFunctionality() {
 System.out.println("This class is derived from " + super.classInfo());
 }

 //main method
 public static void main(String[] args) {
 DerivedClass dc= new DerivedClass();
 dc.extraFunctionality();
 }

}

—

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Related Posts

Download Greenfoot Windows

Java Tutorials /

Download & Install Greenfoot on Windows

Java Tutorials /

Java Static Code Analysis

Java Tutorials /

Java Testing Tools

Java Tutorials /

Handle Multiple Exceptions in Java

Exceptions_In_Java

Java Tutorials /

Exceptions in Java Programs

‹ Java Vector Class› Java Abstract Class with Examples

Recent Posts

  • Update draw.io on Windows
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com