• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Introduction to Computer Science

Module 12. Interfaces and Abstract Classes

We have introduced the 4 pillars of object orientation, and how they constitute the fundamentals of object-oriented design. This module presents more details on how to make use of the concept of abstraction through Java constructs called interfaces and abstract classes, the syntax and general principles of using them in our programs.

Interfaces

Abstract classes

More details on how abstract classes are designed in Java can be found on the Oracle Abstract classes tutorial available on this link.

Exercise

In this exercise, we will practice the principles of object-oriented design using abstract classes. The exercise is to implement objects that represent different animals, such as dogs and chameleons. Each object should have some common properties that indicate they belong to the same ancestor class, such that they are all of animal type, but also have some specialized properties and methods that define their animal type. We can implement this program by defining a class called Animal class that represents all animals. This call can be abstract, as long as no Animal objects are ever created. The Dog and Chameleon classes can implement the methods of Animal class and actually create objects from each animal type. Please design and implement a Java program that has an abstract class called Animal, with properties : numLegs, thermicCondition and breedingType and two methods called eat() and sleep(). Then, implement two concrete classes that are subclasses of the Animal class: Dog and Chameleon, which should implement the Animal class methods such that they are in line with the specific animal they represent. The Dog class should additionally have the properties name and breed, and methods bark() and run(). The Chameleon class, on the other hand, should have the new properties name and age, and the additional method changecolor(). Please also implement the constructor method for each class such that the properties of the Animal class can be set to the predefined values for each type of animal object.

Solution

We can start with the implementation of the Animal class as below.

The Dog class will add the new properties and methods as however we would like to define the distinction. Here for illustration purposes, we use print methods to define the procedure in each method.

Similarly, the Chameleon class will add its own version of the properties and methods inherited from the Animal class.

If we were to create instances from the Dog class, we can use the constructor method we defined earlier, and call different methods specific to the Dog objects.

Chameleon class will also provide a concrete medium to the Animal class to be able to create specific instances from it, belonging to the Chameleon type defined earlier.

Note that as long as the Animal class stays abstract, we will never be able to create instances from it. All extensions, or subclasses, of the abstract Animal class will need to implement all of its abstract methods and constructors if there will be instances created from them.

Primary Sidebar

List of Contents

  • Course Description
  • Module 1. Introduction to Computers and Programming
  • Module 2. The Java language
  • Module 3. Variables and Data Types
  • Module 4. Logical expressions and conditional executing
  • Module 5. Characters and Strings
  • Module 6. Loops
  • Module 7. Arrays
  • Module 8. Methods
  • Module 9. Introduction to the Object-Oriented Programming Paradigm
  • Module 10. Classes and Objects
  • Module 11. Inheritance and Polymorphism
  • Module 12. Interfaces and Abstract Classes
  • Module 13. Exception handling
  • Module 14. File I/O
  • Module 15. Recursion
  • Instructions for software installation
  • Self-assessment quizzes

Footer

This website has been created by Duygu Ataman, Devesh Devendra and Akash Harikrishanan. We thank New York University professors Craig Kapp, Anasse Bari and Gizem Kayar for sharing their feedback and resources to implement a successful online learning platform.

Contact

E-mail: ataman@nyu.edu

www.duyguataman.com

Dr. Duygu Ataman

Assistant Professor and Faculty Fellow, Courant Institute for Mathematical Sciences

Copyright © 2025 · eleven40 Pro on Genesis Framework · WordPress · Log in