• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » SubClass In Java & Java Subclass Example

SubClass In Java & Java Subclass Example

Last Updated on: May 15, 2021 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Subclass In Java
  • Subclass Definition
  • Define Subclass
  • What is a subclass in java?
  • how to create a subclass in java
  • Advantages Of Using Subclass
  • Java Subclass Example

Subclass In Java

Welcome to the post of Java tutorial. in our previous post, we have discussed inheritance in Java. And in this post, we’re going to discuss subclass.

As we know that in Java every class is built from another Java class except the object class. Here the newly created class is called a subclass. The important thing you need to remember is A subclass inherits state and behavior from all of its ancestors.

Subclass Definition

A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

Define Subclass

When you are coming for an interview that time you may face technical java interview questions round define subclass. So we are plan to share more information about the java subclass or subclass java in detail.

What is a subclass in java?

When there is a relationship between two objects in Java, there a name given to the class that is inheriting the methods and variables of the superclass. That’s why the subclass is a more specialized version of the superclass. And the Subclass also called derived classes, child classes, or extended classes.

how to create a subclass in java

To create a subclass we need to use the keyword “extends”. After that, you need to follow the “extends” keyword with the parent class which you want to extend.

For Example:

Public class SubClassName extends ParentClassName

Advantages Of Using Subclass

There are several advantages of using Subclass in Java, like:

  • Reuse of code: Through inheritance, a subclass can reuse methods that already exist in a superclass.
  • Specialization: In a subclass, you can add new methods to handle cases that the superclass does not handle. You can also add new data items that the superclass does not need.
  • Change in action: A subclass can override a method that it inherits from a superclass by defining a method of the same signature like that in the superclass. When you override a method, you might make only a few minor changes or completely change what the method does.

Java Subclass Example

package com.java.Softwaretestingblog;
class superclass
{
   public void show()
   {
      System.out.println("Parent Class");
   }
}
public class SubclassExample extends superclass{
   SubclassExample()
   {
      super.show();
   }
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      SubclassExample obj=new SubclassExample();
   }

Output:

Parent Class

Ref: Article

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers