Parent Classes

A general class in Java is called a parent class. (It may also be referred to as a super class or a base class). For example, a class that defines a general person might look like this:

public class Person 
{
    protected String name;
    protected int age;

    public Person(String name, int age) 
    {
        this.name = name;
        this.age = age;
    }

    public void print() 
    {
        System.out.println("I am a person.");
    }
}

This class looks pretty typical except for the “protected” keyword. Protected is another visibility modifier (like private and public), but it’s specifically for inheritance. A protected variable or method is one that is only visible inside the class or by any child classes. (So, if we decide to make a more specific type of person, we will be able to see the name and age.)