The purpose of creating an abstract class is to model an abstract situation.

Example: You work for a company that has different types of customers: domestic, international, business partners, individuals, and so on. It well may be useful for you to "abstract out" all the information that is common to all of your customers, such as name, customer number, order history, etc., but also keep track of the information that is specific to different classes of customer. For example, you may want to keep track of additional information for international customers so that you can handle exchange rates and customs-related activities, or you may want to keep track of additional tax-, company-, and department-related information for business customers.

Modeling all these customers as one abstract class ("Customer") from which many specialized customer classes derive or inherit ("InternationalCustomer," "BusinessCustomer," etc.) will allow you to define all of that information your customers have in common and put it in the "Customer" class, and when you derive your specialized customer classes from the abstract Customer class you will be able to reuse all of those abstract data/methods.This approach reduces the coding you have to do which, in turn, reduces the probability of errors you will make. It also allows you, as a programmer, to reduce the cost of producing and maintaining the program.

In this assignment, you will analyze Java code that declares one abstract class and derives three concrete classes from that one abstract class. You will read through the code and predict the output of the program.

Read through the linked Java code carefully.

Predict the result of running the Java code. Writeyour prediction into a Microsoft Word document, focusing specifically on what text you think will appear on the console after running the Java code.

In the same Word document, answer the following question:

  • Why would a programmer choose to define a method in an abstract class, such as the Animal constructor method or the getName() method in the linked code example, as opposed to defining a method as abstract, such as the makeSound() method in the linked example?

Given code:

/**********************************************************************
* Program: Analyze Assignment
* Purpose: Analyze the coding for an abstract class
* and two derived classes, including overriding methods
* Programmer: Iam A. Student
* Class: Java Programming
* Creation Date: December 13, 2017
*
* Comments:
* Notice that in the abstract Animal class shown here, one method is
* concrete (the one that returns an animal's name) because all animals can
* be presumed to have a name. But one method, makeSound(), is declared as
* abstract, because each concrete animal must define/override the makeSound() method
* for itself--there is no generic sound that all animals make.
/**********************************************************************

package mytest;

// Animal is an abstract class because "animal" is conceptual
// for our purposes. We can't declare an instance of the Animal class,
// but we will be able to declare an instance of any concrete class
// that derives from the Animal class.
abstract class Animal {

// All animals have a name, so store that info here in the superclass.
// And make it private so that other programmers have to use the
// getter method to access the name of an animal.
private final String animalName;

// One-argument constructor requires a name.
public Animal(String aName) {
animalName = aName;
}

// Return the name of the animal when requested to do so via this
// getter method, getName().
public String getName() {
return animalName;
}

// Declare the makeSound() method abstract, as we have no way of knowing
// what sound a generic animal would make (in other words, this
// method MUST be defined differently for each type of animal,
// so we will not define it here--we will just declare a placeholder
// method in the animal superclass so that every class that derives from
// this superclass will need to provide an override method
// for makeSound()).
public abstract String makeSound();
};

// Create a concrete subclass named "Dog" that inherits from Animal.
// Because Dog is a concrete class, we can instantiate it.
class Dog extends Animal {

// This constructor passes the name of the dog to
// the Animal superclass to deal with.
public Dog(String nameOfDog) {
super(nameOfDog);
}

// This method is Dog-specific.
@Override
public String makeSound() {
return ("Woof");
}
}

// Create a concrete subclass named "Cat" that inherits from Animal.
// Because Cat is a concrete class, we can instantiate it.
class Cat extends Animal {

// This constructor passes the name of the cat on to the Animal
// superclass to deal with.
public Cat(String nameOfCat) {
super(nameOfCat);
}

// This method is Cat-specific.
@Override
public String makeSound() {
return ("Meow");
}

}

class Bird extends Animal {

// This constructor passes the name of the bird on to the Animal
// superclass to deal with.
public Bird (String nameOfBird) {
super(nameOfBird);
}

// This method is Bird-specific.
@Override
public String makeSound() {
return ("Squawk");
}

}

public class MyTest {
public static void main(String[] args) {

// Create an instance of the Dog class, passing it the name "Spot."
// The variable aDog that we create is of type Animal.
Animal aDog = new Dog("Spot");

// Create an instance of the Cat class, passing it the name "Fluffy."
// The variable aCat that we create is of type Animal.
Animal aCat = new Cat("Fluffy");

// Create an instance of (instantiate) the Bird class.
Animal aBird = new Bird("Tweety");

//Exercise two different methods of the aDog instance:
// 1) getName() (which was defined in the abstract Animal class)
// 2) makeSound() (which was defined in the concrete Dog class)
System.out.println("The dog named " + aDog.getName() + " will make this sound: " + aDog.makeSound());

//Exercise two different methods of the aCat instance:
// 1) getName() (which was defined in the abstract Animal class)
// 2) makeSound() (which was defined in the concrete Cat class)
System.out.println("The cat named " + aCat.getName() + " will make this sound: " + aCat.makeSound());

System.out.println("The bird named " + aBird.getName() + " will make this sound: " + aBird.makeSound());
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.