Answer the following questions: - ArrayStack is a public class. What does this m
ID: 3866231 • Letter: A
Question
Answer the following questions:
- ArrayStack is a public class. What does this mean in terms of the overall visibility of the class? Why is this a good design decision? [2]
- All class variables are of type protected. What does this mean in terms of their overall visibility? [1]
- ArrayStack stores objects of type Object rather than specific types like int or float. Why is this good idea? How is this design decision linked to the concept of an explicit cast as a means to store and retrieve any kind of object types on such a stack? [3]
public class ArrayStack implements Stack actual capacity of the stack array protected int capacity; protected static final int CAPACITY protected Object S: protected int top =-1; 1000;// default array capacity II array used to implement the stack II index for the top of the stack public ArrayStacko II default constructor: creates stack with default capacity this(CAPACITY); public void push(Object element) f if (isFullO) return; top++ S[top]- element;Explanation / Answer
a) ArrayStack is a public class meaning that it is visible across all the packages. it is good design because any one who wants to use the functionality of ArrayStack can create the instance of this class and use it.
b) Protected meaning that the member variables can be accessed by any subclass of ArrayStack class and also it can be acceses in the same package.
c)
Why is this a good idea ? Because object is super type of all the classes. so with any type of objects this Object[] will work.
How is this design decision linked to the concept of an explicit cast as a means to store and retrieve any kind of object types on such a stack?
Because array S is of type object. and whenver you are storing any object or retrieving the object, you need to explicitly cast that object otherwise you can not know the type of your object
e.g you want to store the objects of Integer in Object[] s, then while adding you should add cast your object to INteger and then add it and while retriving the objects from stack you should explicitly cast to integer.
Cheers :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.