Question

In: Computer Science

The purpose of creating an abstract class is to model an abstract situation. Example: You work...

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. Write your 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?

CODE:

/**********************************************************************

*           Program:          PRG/421 Week 1 Analyze Assignment

*           Purpose:           Analyze the coding for an abstract class

*                                   and two derived classes, including overriding methods

*           Programmer:     Iam A. Student

*           Class:               PRG/421r13, Java Programming II

*           Instructor:        

*           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());

}

}

Solutions

Expert Solution

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());

}

// The variable is of type Animal but it refers to the object of type Dog
// This is possible since Animal is a base class of Dog and a base class reference variable can point to its derived class objects

Animal aDog = new Dog("Spot");  // this creates a Dog object with name Spot

// Similarly, this creates a Cat object with name Fluffy
Animal aCat = new Cat("Fluffy");

Similarly, this creates a Bird object with name Tweety
Animal aBird = new Bird("Tweety");

System.out.println("The dog named " + aDog.getName() + " will make this sound: " + aDog.makeSound());

// In this statement aDog.getName() will return the name of the Dog i.e Spot and makeSound will call the method defined in the Dog class. Since it is an abstract method of Animal class so the method which will be called depends on the derived class object type to which it refers (and in this case it refers to Dog class)
// since aDog is an Object of Dog class and will print Woof
// Output: The dog named Spot will make this sound: Woof

System.out.println("The cat named " + aCat.getName() + " will make this sound: " + aCat.makeSound());

// In this statement aCat.getName() will return the name of the Cat i.e Fluffy and makeSound will call the method defined in the Cat class. Since it is an abstract method of Animal class so the method which will be called depends on the derived class object type to which it refers (and in this case it refers to Cat class)
// since aCat is an Object of Dog class and will print Meow
// Output: The cat named Fluffy will make this sound: Meow

System.out.println("The bird named " + aBird.getName() + " will make this sound: " + aBird.makeSound());

// In this statement aBird.getName() will return the name of the Bird i.e Tweety and makeSound will call the method defined in the Bird class. Since it is an abstract method of Animal class so the method which will be called depends on the derived class object type to which it refers (and in this case it refers to Bird class)
// since aBird is an Object of Bird class and will print Squawk
// Output: The bird named Tweety will make this sound: Squawk

Output of the above program:

The dog named Spot will make this sound: Woof
The cat named Fluffy will make this sound: Meow
The bird named Tweety will make this sound: Squawk

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?

Programmers choose to define method in an abstract class, such as the Animal constructor or method the getName() method in the above example, as opposed to defining a method as abstract, such as the makeSound() method.

  • In abstract classes all the variables and methods are declared which are common to all the derived classes that inherits it. While some of the methods defined in the abstract class are abstract methods i.e they are declared in the base class but not defined , it is the responsibility of the derived class that inherits them to define those methods, whereas some of the methods may be also defined in the abstract class.
  • The methods defined in the abstract class are those whose functionality remains the same in all its derived class. As given in the above example. Animal constructor and getName() method is defined in the abstract Animal class. Animal constructor is defined to initialize the name of the Animal of all its derived class when it is created. Since all the Animal has the property of name, so instead of defining it in all its subclass , it is defined in Animal class since it is a common property. Similarly, method such as getName() is defined it is because the functionality of getName() is to return the name of the Animal which is defined in the Animal class. So the functionality of this method will be the same in all its subclass.
  • Whereas the method makeSound() is abstract in Animal class, it is because the sound of different Animals are different and it depends on the type of Animal. As in the given example: Dog makes the sound of Woof whereas Cat makes the sound of Meow. So although all the animals make sound but the sound made by different animals are different and hence it is dependent on the type of Animal and so it should be defined in its subclass and not in the Animal class. But since all animals have the property of making sound, it is declared in the base class Animal.



Related Solutions

1. Can you extend an abstract class? In what situation can you not inherit/extend a class?...
1. Can you extend an abstract class? In what situation can you not inherit/extend a class? 2. Can you still make it an abstract class if a class does not have any abstract methods?
The purpose of this exercise is to practice writing an abstract of an academic work. An...
The purpose of this exercise is to practice writing an abstract of an academic work. An abstract is a concise summary of the topic, goals, content, and argument of an academic work. Learning to write a good abstract is a key step in developing a critique of an academic work—you cannot provide a good critique of an article or book until you are able to first summarize the author’s argument and the evidence she uses to support it. Assignment: For...
Which of the following situations described is a good situation to use an abstract class, and...
Which of the following situations described is a good situation to use an abstract class, and where it would be better to use than either a concrete class or an interface? When some methods in the class are abstract and some are concrete, and some variables in the class need to be private, while other variables need to be public When the inheriting class is closely related to the abstract class, such as with an "is-a" relationship When the objects...
Abstract Cart class import java.util.ArrayList; import java.util.HashMap; /** * The Abstract Cart class represents a user's...
Abstract Cart class import java.util.ArrayList; import java.util.HashMap; /** * The Abstract Cart class represents a user's cart. Items of Type T can be added * or removed from the cart. A hashmap is used to keep track of the number of items * that have been added to the cart example 2 apples or 4 shirts. * @author Your friendly CS Profs * @param -Type of items that will be placed in the Cart. */ public abstract class AbstractCart {...
An Abstract for Tetrahymena. . The purpose of this lab was to test the effect of...
An Abstract for Tetrahymena. . The purpose of this lab was to test the effect of salt concentrations on Tetrahymena length of 200 words We performed three experiments to discern the effects of salt on tetrahymena which are chemotaxis, phagocytosis and secretion, and osmosis.
List one example/situation where the skills approach of leadership will not work.
List one example/situation where the skills approach of leadership will not work.
What is the purpose of extending a class due to inheritance? Provide an example of extending...
What is the purpose of extending a class due to inheritance? Provide an example of extending a class due to inheritance. Provide the code, and explain the processes. Simple Java Programming.
Relate a situation where you know of an unethical situation going on at work or at...
Relate a situation where you know of an unethical situation going on at work or at home that you are concerned about. It might be something that you cannot become directly involved in, but that you know about. Tell as much as you can, but again, it is confidential, and no one except the Professor will see it.
Project 3 Details The purpose of this project is to give you experience with creating and...
Project 3 Details The purpose of this project is to give you experience with creating and using custom objects. In it, you will make a couple custom classes that work in tandem with each other, and call them in your main function. For this project we'll be making something kind of like the Chatbot java file we made in class. You will create a Chatbot class that will contain a number of variables and functions. As you can imagine, a...
A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.
1. Java Abstract Class (2 pts) () • A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.• Following is an example of abstract class:abstract class Book{String title;abstract void setTitle(String s);String getTitle(){return title;}}If you try to create an instance of this class like the following line you will get an error:Book...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT