In Java, a class is a blueprint for objects, inheritance lets a subclass reuse and change what its parent class has, and polymorphism means the method that runs depends on the object, not the variable type. The clearest way to learn all three is one small program, so this post builds a Shape example you can paste into any Java setup and run.
Key takeaways
- A class bundles data (fields) and behaviour (methods), and an object is one instance of it.
- A subclass uses extends to inherit the public and protected members of its parent, but it does not inherit private members or constructors.
- Overriding means the subclass declares a method with the same signature as the parent's, and adding @Override makes the compiler check that you did.
- Polymorphism means a variable of the parent type can hold a subclass object, and Java runs the method of the actual object.
- Most exam errors come from constructors, private fields and confusing the variable's type with the object's type.
What is a class in Java?
A class is a blueprint that defines the data and the actions that its objects will have. Each object made from it, using new, gets its own copy of the fields and can call the methods. A good first habit is to make fields private and expose only the methods other code needs, which is called encapsulation.
In the AP Computer Science A course, this material sits in Unit 3, Class Creation, which College Board lists at 10% to 18% of the exam. If you are choosing between AP courses, my post on Python or Java as a first language covers where Java fits. Check your current course and exam description for exactly which object-oriented topics your year is tested on, since I have not confirmed that detail here.
How does inheritance work in Java?
A subclass is declared with extends and gets the members of its parent class. The Oracle Java tutorial puts it this way: a subclass inherits the public and protected members of its parent, and it does not inherit the private members. It also notes that constructors are not members, so they are not inherited, although a subclass constructor can call the parent's with super.
Every class has exactly one direct superclass, except Object, which sits at the top. If you write no extends, your class is implicitly a subclass of Object. The tutorial's inheritance summary also covers final, which stops a class being subclassed or a method being overridden, and abstract classes, which can be subclassed but not instantiated.
What does a worked example look like?
Here is a small program with a parent class Shape and two subclasses. The numbers are produced by the code, so you can check them yourself. I compiled and ran this exact code before publishing it.
class Shape {
private String name;
public Shape(String name) { this.name = name; }
public double area() { return 0; }
public String describe() {
return name + " with area " + String.format("%.2f", area());
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) { super("Circle"); this.radius = radius; }
@Override
public double area() { return Math.PI * radius * radius; }
}
class Rectangle extends Shape {
private double width, height;
public Rectangle(double width, double height) {
super("Rectangle");
this.width = width;
this.height = height;
}
@Override
public double area() { return width * height; }
}
Shape[] shapes = { new Circle(1.5), new Rectangle(2, 3) };
for (Shape s : shapes) System.out.println(s.describe());
It prints Circle with area 7.07 and then Rectangle with area 6.00. Three things are worth tracing slowly:
- The constructors.
Circlecallssuper("Circle")to set the privatenamefield inShape. It cannot writethis.nameitself, because private members are not inherited. - The override.
area()inShapereturns 0. Each subclass declares anarea()with the same signature and a new body. - The reuse.
describe()is written once inShapeand is never repeated in the subclasses.
What is polymorphism, and where is it in that code?
Polymorphism means one call can run different code depending on the actual object. In the example, shapes is an array of type Shape, but it holds a Circle and a Rectangle. When the loop calls s.describe(), which calls area(), Java uses the version belonging to each object.
The Oracle tutorial describes the rule directly: the Java virtual machine calls the appropriate method for the object that is referred to in each variable, not the method defined by the variable's type, and it calls this virtual method invocation. A useful test: if you add a Triangle class later, the loop does not need to change at all.
What mistakes do students make most often?
These are the ones I would check first in any Java homework or past paper answer. They come from how the language works, not from a study of student results.
- Touching a private field from a subclass. Use a public or protected getter, or pass the value up through
super(...). - Forgetting the
supercall. If the parent has no no-argument constructor, the subclass constructor must call a parent constructor that exists. - Mixing up overriding and overloading. Overriding keeps the same signature in a subclass. Overloading uses the same name with different parameters.
- Judging by the variable type. With
Shape s = new Circle(1.5);, the calls.area()runs theCircleversion. The variable type decides which methods you are allowed to call, and the object decides which one runs. - Leaving out
@Override. Without it, a typo such asaera()quietly makes a new method instead of an override. The Oracle pages do not require the annotation, but I add it every time because the compiler then catches the typo.
How should you practise this?
Type the Shape program from memory, then change it. Add a Triangle, add a perimeter() method, and make Shape abstract so it cannot be created directly. After each change, predict the output before you run it, and write down any prediction you got wrong. That is the same loop I describe in how to use past papers for computer science revision.
If you would like someone to check your classes with you, my computer science lessons cover AP CSA and other Java courses, and you can book a free call to try one.
Frequently asked questions
What is the difference between a class and an object in Java?
A class is the blueprint, written once, that defines fields and methods. An object is a specific instance made with new, and each object holds its own values for the fields.
Is polymorphism the same as overriding?
No, but they work together. Overriding is a subclass redefining a parent method. Polymorphism is the behaviour you see when a parent-type variable refers to different subclass objects and each one runs its own version of the method.
Can a Java class extend more than one class?
No. Every class has one direct superclass, which Oracle calls single inheritance. A class can still implement several interfaces, which is the usual way to share behaviour across unrelated classes.
Sources
- Oracle Java Tutorials: Inheritance (subclasses and members)
- Oracle Java Tutorials: Polymorphism
- Oracle Java Tutorials: Summary of Inheritance
- College Board: AP Computer Science A course overview