What is a Subclass

Master C picture Master C · May 3, 2011 · Viewed 115.4k times · Source

What is a "subclass" in java?

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

Answer

Jeremy picture Jeremy · May 3, 2011

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.