46

What is a "subclass" in java?

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

Bulwersator
  • 1,102
  • 2
  • 12
  • 30
Master C
  • 1,536
  • 4
  • 14
  • 19
  • Take a look at the beginner links provided on [java tag wiki](http://stackoverflow.com/tags/java/info) . – CoolBeans May 03 '11 at 00: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. – Brian Burns Mar 26 '14 at 15:13

8 Answers8

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.

Jeremy
  • 22,188
  • 4
  • 68
  • 81
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?

travega
  • 8,284
  • 16
  • 63
  • 91
3

Subclass is to Class as Java is to Programming Language.

user541686
  • 205,094
  • 128
  • 528
  • 886
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();
    }
}
tavalendo
  • 857
  • 2
  • 11
  • 30
Pih
  • 2,282
  • 15
  • 20
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

Alan
  • 45,915
  • 17
  • 113
  • 134
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.

Femaref
  • 60,705
  • 7
  • 138
  • 176
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.

bpanulla
  • 2,988
  • 1
  • 19
  • 23
  • 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 03 '11 at 00: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 03 '11 at 00: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.

Bill Myte
  • 11
  • 1
  • 1
  • 3