Designing Effective Object-Oriented Code Flashcards
What is the relationship between classes in modeling, and how is it implemented in programming languages?
In modeling, the relationship between classes involves specialization, which is implemented in programming languages through inheritance. Inheritance entails deriving a new class (subclass) from an existing one (superclass).
How can you determine if there is a genuine inheritance relationship between two classes?
A reliable test is to try to say “a subclass is a superclass”. If this statement is clearly true, there is an inheritance relationship. If it sounds strange or not true, there is no inheritance relationship, and it should not be implemented.
(“A cargo plane is a plane”)
What components does a subclass inherit from its superclass?
A subclass automatically inherits all components of its superclass. For example, a passenger plane class inherits attributes like manufacturer and type from the plane class.
Why could a subclass possibly not access its own manufacturer and type attributes, even though they are inherited from the superclass?
The manufacturer and type attributes of the superclass may be private, so they are not directly accessible to the subclass. However, there are ways to allow a subclass to access superclass components without making them fully public.
How is inheritance implemented in ABAP?
inheritance is implemented using the INHERITING FROM addition in the class definition statement.
What is the relationship between a subclass and a superclass in terms of inheritance?
A subclass is derived from a superclass, meaning it inherits all the components of the superclass.
What is the restriction on the number of direct superclasses a subclass may have?
A subclass may only have one direct superclass.
How does a subclass extend its functionality or modify existing components inherited from the superclass?
by declaring new components, accessing protected components of the superclass, redefining methods, and defining new constructors.
What is the purpose of the PROTECTED SECTION in a class definition?
The PROTECTED SECTION allows a superclass to grant access to certain attributes or methods to its subclasses without making them fully public.
What is the significance of the REDEFINITION addition when redefining methods in a subclass?
The REDEFINITION addition indicates that the method in the subclass is overriding the implementation of the same-named method in the superclass.
How is the constructor of a subclass related to the constructor of its superclass?
The constructor of a subclass must call the constructor of its superclass to ensure that the superclass instance is properly initialized before setting the subclass-specific attributes.
What is the sequence of operations when implementing a new constructor in a subclass?
The sequence involves first calling the constructor of the superclass, then initializing subclass-specific attributes after ensuring the superclass instance is properly created.
What is the role of the static constructors in the sequence of constructor calls?
Static constructors are called sequentially from the top of the inheritance hierarchy before the instance constructor of the subclass is called. They initialize static attributes and perform other class-level setup tasks.
How can you call the implementation of a method in the superclass when redefining it in a subclass?
You can call the implementation in the superclass using the implicit object reference super, which points to the superclass within the current instance.
How do you instantiate a subclass in ABAP?
To instantiate a subclass, you declare a reference variable with the type of the relevant class, and then use the NEW operator to create the instance.
You must pass values to all obligatory parameters of the constructor associated with that subclass.
What is the significance of using a reference variable with the type of a superclass?
Such a reference variable can hold references to objects with the type of a subclass.
What components can be accessed when using a reference variable with the type of a superclass?
Only the components defined in the superclass can be accessed, even if the reference points to an object of the subclass.
What is the term for assigning object references to a reference variable of a different type?
Casting.
What is an up-cast in the context of object references?
It refers to the assignment of a reference that points to a subclass to a reference variable with the type of a superclass.
What limitations are there when using a reference variable with the type of a superclass to manage a subclass instance?
You cannot access components that belong to the subclass because the superclass does not know about them.
Can you still access methods when using a reference variable of the superclass type to manage a subclass instance?
Yes, you can access methods whose original definition is contained in the superclass.
How does the system determine which implementation of a method to call when using a reference variable of the superclass type to manage a subclass instance?
At runtime, the system looks at the type of object the reference variable is pointing to and calls the implementation from the corresponding class.
Can you always assign a reference variable with the type of a subclass to a reference variable with the type of a superclass?
Yes
Why is assigning reference variables in the opposite direction (from superclass to subclass) more tricky?
It’s more tricky because not every superclass reference necessarily points to a subclass object. Therefore, the syntax check cannot be certain about the type of object the reference variable holds at runtime.