Designing Effective Object-Oriented Code Flashcards

1
Q

What is the relationship between classes in modeling, and how is it implemented in programming languages?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you determine if there is a genuine inheritance relationship between two classes?

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What components does a subclass inherit from its superclass?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why could a subclass possibly not access its own manufacturer and type attributes, even though they are inherited from the superclass?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How is inheritance implemented in ABAP?

A

inheritance is implemented using the INHERITING FROM addition in the class definition statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the relationship between a subclass and a superclass in terms of inheritance?

A

A subclass is derived from a superclass, meaning it inherits all the components of the superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the restriction on the number of direct superclasses a subclass may have?

A

A subclass may only have one direct superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does a subclass extend its functionality or modify existing components inherited from the superclass?

A

by declaring new components, accessing protected components of the superclass, redefining methods, and defining new constructors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the purpose of the PROTECTED SECTION in a class definition?

A

The PROTECTED SECTION allows a superclass to grant access to certain attributes or methods to its subclasses without making them fully public.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the significance of the REDEFINITION addition when redefining methods in a subclass?

A

The REDEFINITION addition indicates that the method in the subclass is overriding the implementation of the same-named method in the superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is the constructor of a subclass related to the constructor of its superclass?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the sequence of operations when implementing a new constructor in a subclass?

A

The sequence involves first calling the constructor of the superclass, then initializing subclass-specific attributes after ensuring the superclass instance is properly created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the role of the static constructors in the sequence of constructor calls?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can you call the implementation of a method in the superclass when redefining it in a subclass?

A

You can call the implementation in the superclass using the implicit object reference super, which points to the superclass within the current instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you instantiate a subclass in ABAP?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the significance of using a reference variable with the type of a superclass?

A

Such a reference variable can hold references to objects with the type of a subclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What components can be accessed when using a reference variable with the type of a superclass?

A

Only the components defined in the superclass can be accessed, even if the reference points to an object of the subclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the term for assigning object references to a reference variable of a different type?

A

Casting.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is an up-cast in the context of object references?

A

It refers to the assignment of a reference that points to a subclass to a reference variable with the type of a superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What limitations are there when using a reference variable with the type of a superclass to manage a subclass instance?

A

You cannot access components that belong to the subclass because the superclass does not know about them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Can you still access methods when using a reference variable of the superclass type to manage a subclass instance?

A

Yes, you can access methods whose original definition is contained in the superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

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?

A

At runtime, the system looks at the type of object the reference variable is pointing to and calls the implementation from the corresponding class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Can you always assign a reference variable with the type of a subclass to a reference variable with the type of a superclass?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Why is assigning reference variables in the opposite direction (from superclass to subclass) more tricky?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What happens if you attempt to assign a superclass reference to a subclass reference?

A

The assignment leads to a syntax error because the syntax check cannot be sufficiently sure that the superclass reference holds a reference to a subclass object at that moment.

26
Q

How can you assign a superclass reference to a subclass reference?

A

You can use the CAST operator to cast the superclass reference to the subclass reference, treating the object as though it has a different type.

27
Q

What does casting mean in this context?

A

Casting involves looking at an object as though it belongs to a different type than its actual type.

28
Q

What is the risk associated with using the CAST operator?

A

The assignment made using the CAST operator may cause a runtime error if the actual object referenced by the superclass reference is not an instance of the subclass type being cast to.

29
Q

How can you ensure the safety of a down-cast operation?

A

You can use the logical expression IS INSTANCE OF to verify that the object referenced by a superclass reference is actually an instance of the correct subclass

30
Q

What are interfaces in programming?

A

Interfaces allow you to define a contract for classes to implement, specifying methods, attributes, types, and constants without providing implementations.

31
Q

Can interfaces be instantiated?

A

No, interfaces cannot be instantiated. They only serve as templates for classes to implement.

32
Q

What are two important uses of interfaces?

A

Providing a unified approach to different classes by defining common services in an interface.

Providing a simplified approach to a complex class by exposing a specific set of methods to a program

33
Q

What must a class do when implementing an interface?

A

When a class implements an interface, it must provide implementations for all the methods declared in the interface.

34
Q

How does the definition of an interface differ from that of a class in ABAP?

A

An interface definition does not include a DEFINITION addition, and all components within an interface are public by default.

35
Q

What components can be included in an interface definition?

A

An interface definition can include types, attributes, constants, and methods, both instance and static.

36
Q

What is the purpose of interface methods?

A

Interface methods have a definition but no implementation. The implementation is provided by the class that implements the interface.

37
Q

How are interfaces similar to abstract superclasses?

A

Interfaces provide a way to define common components for classes without a clear “is a” relationship, similar to abstract superclasses.

38
Q

How do you implement an interface in a class in ABAP?

A

To implement an interface in a class, you use the INTERFACES statement. The interface must be included in the public section of the class.

39
Q

Why does including an interface in a class make the program syntactically incorrect at first?

A

Including an interface in a class requires implementing all of its methods. If any methods are missing, the program is syntactically incorrect.

40
Q

How do you implement methods from an interface in a class?

A

In the class implementation, you must implement all methods declared in the interface. When naming the method in the implementation, you must use the fully-qualified name, consisting of the interface name and the method name joined by a tilde.

41
Q

Why is using the fully-qualified name necessary when implementing interface methods?

A

Using the fully-qualified name ensures that there is no ambiguity in method names, as component names must be unique within the class or interface in which they are declared.

42
Q

How do you declare an interface reference variable in ABAP?

A

You declare an interface reference variable by specifying the interface name as the type of the variable.

43
Q

What kinds of objects can you manage using an interface reference?

A

Interface references can contain references to instances of any class that implements the interface.

44
Q

What components can you access when using an interface reference to manage an instance of a class?

A

you can only access the components defined in the interface, not those specific to the implementing class.

45
Q

How do you call a method from an interface using a reference variable with the type of the class?

A

When calling a method from an interface using a reference variable with the type of the class, you must use the fully-qualified name of the method, including the interface name. Alternatively, if you have defined an alias for the method, you can use that.

46
Q

What is casting with interface references?

A

Casting with interface references involves looking at an object as though it were an instance of the interface it implements.

47
Q

What is an up-cast in the context of interfaces?

A

An up-cast involves assigning an instance of a class to an interface reference, which is guaranteed by the fact that the class implements the interface.

48
Q

Why can’t you assign an object from an interface reference to a reference with the type of an implementing class?

A

The syntax check cannot determine whether the actual runtime objects will be compatible, potentially causing a runtime error.

49
Q

What is a factory method in ABAP?

A

A factory method in ABAP is a public static method provided by a class that creates instances of that class and returns them to the user.

50
Q

How is control over instances maintained in ABAP?

A

by using the CREATE PRIVATE addition in the CLASS DEFINITION statement, which prevents users from creating instances themselves using the NEW operator outside the class.

51
Q

Why is a factory method used in ABAP?

A

To allow controlled creation of instances, ensuring that users cannot directly instantiate objects but instead must use the factory method provided by the class.

52
Q

What is the purpose of the singleton pattern?

A

The purpose of the singleton pattern is to ensure that there is only one instance of a class in an application.

53
Q

How does the singleton pattern work?

A

The singleton pattern typically involves providing a mechanism to create a single instance of a class and ensuring that all subsequent requests for instances return the same instance.

54
Q

In what scenarios is the singleton pattern commonly used?

A

The singleton pattern is commonly used when there should be exactly one instance of a class that controls access to a shared resource, manages a global state, or coordinates actions within a system.

55
Q

When you address an object using an interface reference, which elements can you address?

A
All of the elements in the interface

B
All of the elements in the class

C
All of the public elements in the class

A

A
All of the elements in the interface

56
Q

A class lcl_plane has the subclasses lcl_passenger and lcl_cargo. Which of the following statements is true?

A
You can assign an instance of lcl_passenger to a reference variable with type lcl_plane.

B
You can only assign an instance of lcl_passenger to a reference variable with type lcl_passenger

C
You can assign an instance of lcl_passenger to a reference variable with type lcl_cargo.

A

A
You can assign an instance of lcl_passenger to a reference variable with type lcl_plane.

57
Q

Which of the following statements may an interface contain?

A
METHODS

B
METHOD… ENDMETHOD

C
PUBLIC SECTION

D
TYPES

A

A
METHODS

D
TYPES

58
Q

In ABAP, a subclass may have more than one direct superclass?

True or False

A

False

59
Q

Your class lcl_class has a factory method factory. Whichh of the following are properties of factory?

A
It is an instance method

B
It may not have any importing parameters

C
It is a public method

D
Its returning parameter has the type REF TO lcl_class

A

C
It is a public method

D
Its returning parameter has the type REF TO lcl_class

60
Q
A