INH - Creating Superclasses and Subclasses (Lesson)

APCompSci_LessonTopBanner.png

Creating Superclasses and Subclasses

Introduction

Before we delve into inheritance, we need to review classes in Java. A Java program consists of one or more classes. A class is an abstract description of objects. When you write a program, you are writing classes and all the things that go into classes. Your program typically contains commands to create objects (calls to constructors).

Classes Review

A class is like a cookie cutter, objects are like cookies. When you run a program, it creates objects. Those objects interact with one another and cause something to happen. Writing a program is like writing the rules to a game; running a program is like playing the game. You never know how well the rules are going to work until you try them out.

Objects are created by other objects. Programs have a special main method, not part of any object, that is executed in order to get things started. For example,

public static void main (String [] args)
{
Cat rere = new Cat("Pythagorean", 4);
//creates a cat
} 

The special keyword static says that the main method belongs to the class itself, not to objects of the class. Hence, the main method can be “called” before any objects are created. Usually, the main method gets things started by creating one or more objects and telling them what to do.

Here is an example class:

public class Cat
{ //description of a cat goes here...} 

 Classes describe the data held by each of its objects.

public class Cat
{
String name;
int age;
// rest of the class...
} 

A class may describe any number of objects. Examples: “Jellybean”, 3; “Maxie”, 5; “Roxy”, 10; A class may describe a single object, or even no objects at all, such as the Math class. A class may contain methods that describe the behavior of objects.

public class Cat
{
public void speak ()
{
System.out.print1n ("Meow!") ;
}
} 

When we ask a Cat to speak, it says “Meow!” Only Cat objects can speak; the class Cat cannot speak. In the previous example, speak is a method of the Cat class. 

cat saying meow 

Methods contain statements. A statement causes the object to do something.

System.out.print1n ("Meow!"); 

This causes the Cat to “print” (actually, display on the screen) the characters Meow!

Data described in a class exists in all objects of that class. Every Cat has its own name and age. A method may contain local temporary data that exists only until the method finishes.

public void wakeTheFamily ( )
{
int i = 50;   //i is a temporary variable
while  (i >0)
{
speak();
i = i - 1;
}
} 

A constructor is a piece of code that “constructs” or creates a new object of that class. If you don’t write a constructor, Java defines one for you (behind the scenes). If Java does add a constructor, it will be a default constructor with no parameters.

You can write your own constructors:

public class Cat
{
String name;
int age;

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

Things to remember:

  • A program consists of one or more classes.
  • A class is a description of a kind of object.
  • In most cases, it is the objects that do the actual work.
  • A class describes data, constructors, and methods.
  • An object’s data is information about that object.
  • An object’s methods describe how the object behaves.
  • A constructor is used to create objects of the class.
  • Methods (and constructors) may contain temporary data and statements (commands).

Inheritance

Click below to begin the Inheritance learning activity.

 

Inheritance Syntax

public class Animal
{
//private instance variables

//constructors

//methods
}

public class Dog extends Animal
{
//more private instance variables

//constructors

//methods
}
 

When using inheritance to extend a class, notice that we use the keyword extends in the name of the subclass. The subclass can use the instance variables from the superclass if the variables are protected instead of private. When variables are made private, no other class can access them unless using proper accessor methods. Recall the different access specifiers: public, private, and protected. Public means that any class may access, private means that no other class may access, and protected means that only the subclasses may access.

BookIcon.png Click on "Runestone Academy" below to open the required reading that is listed.

Runestone Academy: AP CSA – Java Review Links to an external site.

  • READ: 11.3 – Inheritance

BookIcon.png Click on Introduction to Computer Science Using Java

Introduction to Computer Science Using Java Links to an external site.

  • READ: Chapter 80: Inheritance
  • Complete the quiz for practice.

Click below to begin the Inheritance self-assessment activity.

 

APCompSci_LessonBottomBanner.pngIMAGES CREATED BY GAVS