AP Computer Science A -- Inheritance

Inheritance can be defined as the process where one class acquires the properties or members (methods and fields) of another. With the use of inheritance, the information is made manageable in a hierarchical order. Inheritance is an OOP feature that allows you to develop a class once and then reuse that code over and over as the basis of new classes. The class whose functionality is inherited is called a base class or parent class. The class that inherits the functionality is called a derived class or child class. A derived class can also define additional features that make it different from the base class.


Introduction to Inheritance | Super Reference | Abstract Class & Interface | Polymorphism | Review | Resources


Introduction to Inheritance

Unfortunately Java inheritance is not about a large sum of money that your beloved and rich aunt has left you in her will. Inheritance is a Java feature that is immensely valuable because it allows objects from one class to inherit the features of another class. Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system). The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also. Inheritance represents the IS-A relationship which is also known as a parent-child relationship.


Super Reference

Assignment: Super Reference Video Check

Directions: After watching and studying the video on the Java Inheritance Cycle, complete the Java Inheritance Video check in itsLearning. You will only have a few minutes to complete the check as it is expected that you have reviewed the material before beginning the check.

Assignment: You Do It Exercise 9.1

Directions: Download the instruction sheet and follow the directions. When you are complete, upload the .java file along with a screenshot from the BlueJ terminal window.

father & son inheritanceAssignment: Object-Orientated Programming Terminology Worksheet

Directions:

Review the following terms:

  1. Object-oriented programming
  2. Imperative (procedural) programming
  3. Functional programming
  4. Object
  5. Class
  6. Encapsulation
  7. Abstraction/Information hiding

After viewing the object-orientated programming terminology, complete the Object-Orientated Programming Terminology Worksheet in itsLearning.

Rules of Subclasses Submit your completed UML Diagram to the itsLearning assignment.

Discussion: Inheritance

Directions: Describe in your own words:

  1. What is inheritance?
  2. How does inheritance improve a programmer's productivity?

Be sure to respond to two of your classmates.

Assignment: You Do It Exercise 9.2

Directions: Download the instruction sheet and follow the directions. When you are complete, upload the .java file along with a screenshot from the BlueJ terminal window.


Polymorphism

When you are designing for inheritance, you always want to put the most general states and behaviors as high in the hierarchy as possible. For example, if I were designing a dog class, I might put that a dog has two eyes, four paws, and fur as my states and can walk, can bark, can eat as my behaviors. In Java, a child class is allowed to override a method of a parent class. This means that even though the child inherited a certain way to do something or an attribute from the parent, the child can do it in a different way if it wants to. This is polymorphism. Because not all dogs bark the same way, I may need to override by the bark method or perhaps instead of fur, my dog might have hair so I would need to override the fur variable with a hair variable. This is where polymorphism comes in.

Polymorphism is the ability of derived classes to share common functionality with base class but still define their own unique behavior. Polymorphism allows the objects of a derived class to be treated at runtime as objects of the base class. When a method is invoked at runtime, its exact type is identified, and the appropriate method is invoked from the derived class.

To learn more about Polymorphism, visit http://interactivepython.org/runestone/static/JavaReview/OOBasics/ooPoly.html.

polymorphism

Assignment: Designing for Inheritance Video Check

Directions: After watching and studying the video on the Designing for Inheritance, complete the Designing for Inheritance Video check in itsLearning. You will only have a few minutes to complete the check as it is expected that you have reviewed the material before beginning the check.

Assignment: Inheritance Worksheet

Directions: Download the Inheritance Worksheet. Mark the letter of the best answer to each question. Follow your teacher's instructions for submitting the document.

Assignment: Polymorphism Questions

Directions: Answer the questions in the Polymorphism Questions worksheet in itsLearning.

Assignment: You Do It Exercise 9.3

Directions: Download the instruction sheet and follow the directions. When you are complete, upload the .java file along with a screenshot from the BlueJ terminal window.


Abstract Classes & Methods

Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces.

The abstract class provides a common definition of a base class that can be shared by multiple derived classes including other abstract classes. The abstract class often provides incomplete implementation. To instantiate an abstract class you must inherit from it and complete the implementation. An abstract class must have the keyword abstract in its class declaration. It does not contain any constructors but should contain abstract methods which are methods that have the keyword abstract in the method declaration but do not contain any code. An abstract method can only set a visibility modifier, one of public or protected. That is, an abstract method cannot add static or final modifier to the declaration. An abstract method cannot be declared private because it cannot be accessed outside the current class. An abstract class can have final, non-final, static and non-static variables.

Did you know?

A class can have private constructor. Even an abstract class can have private constructor. By making constructor private, we prevent the class from being instantiated as well as subclassing of that class.


Interface

The interface is a Java feature that is similar in some respects to the abstract class. However, the interface is not a class. It is a reference type. You cannot make an object from an interface but a class can implement one or more interfaces. The main difference between an interface and an abstract class is that an interface only contains method declarations without any implementation. The classes that contain the implementation for the interface use the keyword implements in their class declaration. Although abstract classes and interfaces are no longer in the AP subset and will not be included on the AP exam, they are both fundamental parts of inheritance.

Discussion: Abstract & Interfaces

Directions: Describe in your own words: What are the similarities and differences between abstract classes and interfaces? Please follow your instructor's directions to participate in the discussion.

Assignment: Abstract Class vs. Interface Compare/Contrast

Directions: Compare and contrast the similarities and differences between Java interfaces and abstract classes. Your compare/contrast should include at least 5 similarities and 5 differences. You could create a comparison table or a Venn diagram. The document should be visually appealing with:

Students may complete this assignment on paper or on the computer. Follow your teacher's instructions for submitting your assignment.

Assignment: Interface Worksheet

Directions: Download the Interface Worksheet. Mark the letter of the best answer to each question. Follow your teacher's instructions for submitting the document.

Assignment: Abstract Worksheet

Directions: Download the Abstract Classes Worksheet. Select the letter of the best answer to each question. Follow your teacher's instructions for submitting the document.

Exploring Inheritance (Dogs) Lab

dog blueprint

Directions: Read this short lesson explaining inheritance from Columbus State: Inheritance. The code is especially helpful to review. Then download the Exploring Inheritance (Dogs) lab document. File Dog.java contains a declaration for a Dog class. Save this file to your directory and study it--notice what instance variables and methods are provided. Files Labrador.java and Yorkshire.java contain declarations for classes that extend Dog. Save and study these files as well.

File DogTest.java contains a simple driver program that creates a dog and makes it speak. Study DogTest.java, save it to your directory, and compile and run it to see what it does. Subject your .java files along with a screenshot from the BlueJ terminal window.

dogs


Review

Assignment: Unit 9 Study Guide Worksheet

Directions: Complete the study guide worksheet in itsLearning to review for the unit assessment. This is not a graded assignment.


Resources

The Super Reference: presentation

Designing for Inheritance: presentation

Abstract Classes: presentation

Credits