answersLogoWhite

0

What is sub class in Java?

Updated: 8/11/2023
User Avatar

Wiki User

14y ago

Best Answer

A sub class is paired up with its super class. These can also be called a child and parent class.

The subclass will inherit all of the variables and methods from its superclass. And one superclass can have multiple subclasses, but a subclass can only have one superclass.

Here's an example:

public class childName extends parentName {

...

}

This means that childName will inherit everything from its superclass, which is parentName.

The advantage of this inheritance is that you do not need to copy/paste lots of code for many similar subclasses, and if you want to change something you only need to make one change in the superclass.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

The superclass is the class from which the current class inherits.
The subclass is the class which inherits from the current class.

class ArrayList extends AbstractList {}

Here AbstractList is the superclass of ArrayList and ArrayList is the subclass of AbstractList.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

A subclass in Java is a class that inherits features from a parent class.

class Car {

public void goForADrive() {

System.out.println("driving a car");

}

// more code

}

class Ferrari extends Car {

public void takeFerrariForADrive() {

System.out.println("driving a Ferrari");

}

// more code

}

In the above example Ferrari is the subclass and Car is the super class

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is sub class in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How an exception subclass is created in java explain?

We can create a exception sub class by extending Exception class available in java


Is inheritence in java only based on super class and sub class?

Yes. Inheritance is achieved by using the super class and sub class concept


What is meant by interface in java?

interface is just like a class. its contains abstract methods without any implementation and we cant create object for the class. we can only sub classed . used to achieved multiple inheritance in java.


Extension file for Java?

Java source files have the .java extension, compiled Java class files have the .class extension.


Can you define class as private in java?

Yes. However, ONLY nested classes (as in sub-classes of a top-level class) could be declared as private. The main (top-level) class cannot be private as it couldn't be accessed.


The actions in a java class are called?

The actions in a java class are called methods.


Why you start java program by class?

without class non of the folder can run so the java program should start in class we can use the class without object in java


When java uses compiler?

What i know is java we will use compiler when it want to get class file(file with .class extension) from java file(file with .java extension).


In java why method defined in super class need not to defined in subclass?

In Java, or in any object oriented language such as C++, a method defined in super (parent) class does not need to be defined in a subclass, because that is the primary purpose of inheritance. Object oriented programming allows you to define and declare a class that implements the behavior for an object. Inheritance allows you to refine, or subclass, that class by "reusing" all of the functionality of the parent class into the sub class, adding additional definition and declaration for the sub class. If the subclass needs to change a parent class method, it can overload that method. This is called abstraction.


How do you call main class with in main class in java?

We can't call a class. We always call a method in java.


What class begins java class hierarchy?

Class


What is the way to get rid of Java lang no class deffound error?

To get rid of the Java lang no class deffound error, one must add the class or .jar file which contains this class into the Java classpath. When a Java class is run from the command line, one must add a dot (.)