46

What is a "subclass" in java?

I know about classes and methods, but I do not know about subclasses.

2
  • Take a look at the beginner links provided on java tag wiki .
    – CoolBeans
    May 3, 2011 at 0:09
  • 18
    This page is actually the 2nd Google hit for 'Java subclass'. Stack Overflow often has quicker examples for syntax and such than other sites, though the questions are often closed as not constructive. Mar 26, 2014 at 15:13

8 Answers 8

86

A subclass is a class that extends another class.

public class BaseClass{
    public String getFoo(){
        return "foo";
    }
}

public class SubClass extends BaseClass{
}

Then...

System.out.println(new SubClass().getFoo());

Will print:

foo

This works because a subclass inherits the functionality of the class it extends.

2
  • 3
    This what called inheritance in Java?
    – Master C
    May 2, 2011 at 23:58
  • 12
    +1 For straightforward explanation that anyone can relate to. May 3, 2011 at 0:06
10

A subclass is something that extends the functionality of your existing class. I.e.

Superclass - describes the catagory of objects:

public abstract class Fruit {

    public abstract Color color;

}

Subclass1 - describes attributes of the individual Fruit objects:

public class Apple extends Fruit {

    Color color = red;

}

Subclass2 - describes attributes of the individual Fruit objects:

public class Banana extends Fruit {

    Color color = yellow;

}

The 'abstract' keyword in the superclass means that the class will only define the mandatory information that each subclass must have i.e. A piece of fruit must have a color so it is defines in the super class and all subclasses must 'inherit' that attribute and define the value that describes the specific object.

Does that make sense?

1
  • Red apple? Haha
    – user5698345
    Aug 30, 2017 at 13:33
3

Subclass is to Class as Java is to Programming Language.

3

It is a class that extends another class.

example taken from https://www.java-tips.org/java-se-tips-100019/24-java-lang/784-what-is-a-java-subclass.html, Cat is a sub class of Animal :-)

public class Animal {

    public static void hide() {
        System.out.println("The hide method in Animal.");
    }

    public void override() {
        System.out.println("The override method in Animal.");
    }
}

public class Cat extends Animal {

    public static void hide() {
        System.out.println("The hide method in Cat.");
    }

    public void override() {
        System.out.println("The override method in Cat.");
    }

    public static void main(String[] args) {
        Cat myCat = new Cat();
        Animal myAnimal = (Animal)myCat;
        myAnimal.hide();
        myAnimal.override();
    }
}
2

A subclass in java, is a class that inherits from another class.

Inheritance is a way for classes to add specialized behavior ontop of generalized behavior. This is often represented by the phrase "is a" relationship.

For example, a Triangle is a Shape, so it might make sense to implement a Shape class, and have the Triangle class inherit from it. In this example, Shape is the superclass of Triangle and Triangle is the subclass of Shape

2

If you have the following:

public class A
{
}

public class B extends A
{
}

then B is a subclass of A, B inherits from A. The opposite would be superclass.

1
  • Does public class B : A means public class B extends A ??
    – Master C
    May 2, 2011 at 23:58
2

Think of a class as a description of the members of a set of things. All of the members of that set have common characteristics (methods and properties).

A subclass is a class that describes the members of a particular subset of the original set. They share many of characteristics of the main class, but may have properties or methods that are unique to members of the subclass.

You declare that one class is subclass of another via the "extends" keyword in Java.

public class B extends A
{
...
}

B is a subclass of A. Instances of class B will automatically exhibit many of the same properties as instances of class A.

This is the main concept of inheritance in Object-Oriented programming.

2
  • So an abstract class must contain at least 1 mandatory information that each subclass must have ? including methods ? BUT can also contains its own information and methods that I am not obligate to use (but are still an option) ?
    – Master C
    May 3, 2011 at 0:19
  • I don't think abstract classes need to have any properties in particular. Sometimes just the fact that a class inherits from an abstract class is all that you want. (see "polymorphism")
    – bpanulla
    May 3, 2011 at 0:35
0

A sub class is a small file of a program that extends from some other class. For example you make a class about cars in general and have basic information that holds true for all cars with your constructors and stuff then you have a class that extends from that on a more specific car or line of cars that would have new variables/methods. I see you already have plenty of examples of code from above by the time I get to post this but I hope this description helps.

1
  • It does not necessarily have to be an own file though.
    – F_V
    Nov 24, 2016 at 13:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.