7a

Advanced Placement

Computer Science Lab

 

 

Introduction to Inheritance

 

Introduction

 

 

 

Inheritance is a basic technique in object oriented programming for organizing and creating classes.  Its purpose is to allow the programmer to reuse classes or parts of classes so that with each new application the code does not have to be regenerated from scratch.

 

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Building now becomes the superclass of Residential, Commercial, and Agricultural. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses

 

 

 

 

 

public class Building

{

     protected String type = "";

 

     public void printType()

     {

          System.out.print("Building is " + type);

          System.out.println();

 

     }//end method

 

}//end class

 

 

 

 

 

 

 

public class Residential extends Building

{

 

     private String model = "house

";

 

     public Residential()

     {

          super.type = "Residential";

         

     }

 

     public void setModel()

     {

         

     }

 

 

 

 

public String type = "";

 

     public void printType()

     {

          System.out.print("Building is " + type);

          System.out.println();

 

     }

 

}

 

 

 

 

 

 

 

 

 

 

 

Programming Assignment

 

 

A rental company wants a program to develop an inventory of vehicles.

Create a graphics program that includes a hierarchy of different of vehicles.  Include a GUI that allows the user to input a type of vehicle and then print out available vehicles.  Vehicle attributes could include weight, MPG, number of passengers, manufacturer, rental rate, etc..  Overwrite the "toString" method in each class in such a way as to be able to quickly print out the inventory of vehicles.  Program must include at least one parent class and three child classes, a driver and the GUI.

 

Turn in a program listing your source code.  Source code should use proper formatting.  For this assignment you will also need to show me your program running.