Lesson 3: Object-Oriented Programming Flashcards

1
Q

What are classes and objects?

A

A class is a template that defines the form of an object. It specifies both the data and the code that will operate on that data. Java uses a class specification to construct objects. Objects are instances of a class. Thus، a class is essentially Page 107a set of plans that specify how to build an object. It is important to be clear on one issue: a class is a logical abstraction. It is not until an object of that class has been created that a physical representation of that class exists in memory.

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

What are members/instance variables?

A

methods and variables that constitute a class are called members of the class. The data members are also referred to as instance variables.

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

What is the general form of a class definition?

A

A class is created by using the keyword class. A simplified general form of a class definition is shown here: class classname { // declare instance variables type var1; type var2; // … type varN; // declare methods type method1(parameters) { // body of method } type method2(parameters) { // body of method } // … type methodN(parameters) { // body of method } }

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

What does the dot operator do in Java?

A

The dot operator links the name of an object with the name of a member. The general form of the dot operator is shown here: object.member Thus، the object is specified on the left، and the member is put on the right. For example، to assign the fuelcap variable of minivan the value 16، use the following statement:

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

When you have multiple classes، which one should come first? The one with the main، or the one(s) without it?

A

Either class can be the first one in the file.

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

Are the instance variables in one object the same as the instance variables in another object?

A

each object has its own copies of the instance variables defined by its class. Thus، the contents of the variables in one object can differ from the contents of the variables in another. There is no connection between the two objects except for the fact that they are both objects of the same type.

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

How do you declare an instance that refers to an object?

A

This is done by using the new operator

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

What does the “new” operator do?

A

The new operator dynamically allocates (that is، allocates at run time) memory for an object and returns a reference to it. This reference is، essentially، the address in memory of the object allocated by new. This reference is then stored in a variable. Thus، in Java، all class objects must be dynamically allocated.

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

How many tasks does a method perform?

A

In well-written Java code، each method performs only one task.

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

What goes directly after a method’s name?

A

A method will have parentheses after its name

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

What is the general form of a method?

A

The general form of a method is shown here: ret-type name( parameter-list ) { // body of method } Here، ret-type specifies the type of data returned by the method. This can be any valid type، including class types that you create. If the method does not return a value، its return type must be void. The name of the method is specified by name. This can be any legal identifier other than those already used by other items within the current scope. The parameter-list is a sequence of type and identifier pairs separated by commas. Parameters are essentially variables that receive the value of the arguments passed to the method when it is called. If the method has no parameters، the parameter list will be empty.

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

Why does a method not need a dot operator when using an instance variables defined by it’s class?

A

When a method uses an instance variable that is defined by its class، it does so directly، without explicit reference to an object and without use of the dot operator. This is easy to understand if you think about it. A method is always invoked relative to some object of its class. Once this invocation has occurred، the object is known. Thus، within a method، there is no need to specify the object a second time.

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

What are the two forms of return statements?

A

There are two forms of return—one for use in Page 115void methods (those that do not return a value) and one for returning values.

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

What are the two ways in which a void method can return?

A

A void method can return in one of two ways—its closing curly brace is reached، or a return statement is executed.

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

What type of return returns a value?

A

Methods return a value to the calling routine using this form of return: return value; Here، value is the value returned. This form of return can be used only with methods that have a non-void return type. Furthermore، a non-void method must return a value by using this form of return.

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

What is a parameter?

A

Inside the method، the variable that receives the argument is called a parameter. Parameters are declared inside the parentheses that follow the method’s name.

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

How many parameters can a method have?

A

A method can have more than one parameter. Simply declare each parameter، separating one from the next with a comma. boolean methodName(int a، double b، float c)

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

What is a constructor and what do you use it for?

A

A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However، constructors have no explicit return type. Typically، you will use a constructor to give initial values to the instance variables defined by the class، or to perform any other startup procedures required to create a fully formed object.

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

Which classes have constructors?

A

All classes have constructors، whether you define one or not، because Java automatically provides a default constructor.

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

What happens when you set an object to the same value as a class? What if you set another object to the same value as the previous object?

A

This constructor assigns the instance variable x of MyClass the value 10. This constructor is called by new when an object is created.

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

How do you add parameters to a constructor?

A

Parameters are added to a constructor in the same way that they are added to a method: just declare them inside the parentheses after the constructor’s name. In this version of the program، the MyClass( ) constructor defines one parameter called i، which is used to initialize the instance variable، x.

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

What is the general form of the “new” operator?

A

the new operator has this general form: class-var = new class-name(arg-list); Here، class-var is a variable of the class type being created. The class-name is the name of the class that is being instantiated. The class name followed by a parenthesized argument list (which can be empty) specifies the constructor for the class. If a class does not define its own constructor، new will use the default constructor supplied by Java. Thus، new can be used to create an object of any class type. The new operator returns a reference to the newly created object، which (in this case) is assigned to class-var.

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

How does Java’s garbage system work?

A

Java’s garbage collection system reclaims objects automatically—occurring transparently، behind the scenes، without any programmer intervention. It works like this: When no references to an object exist، that object is assumed to be no longer needed، and the memory occupied by the object is released. This recycled memory can then be used for a subsequent allocation.

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

Why don’t I need to use new for variables of the primitive types، such as int or float?

A

Java’s primitive types are not implemented as objects. Rather، because of efficiency concerns، they are implemented as “normal” variables. A variable of a primitive type actually contains the value that you have given it. As explained، object variables are references to the object. This layer of indirection (and other object features) adds overhead to an object that is avoided by a primitive type.

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

When does garbage collection occur?

A

Garbage collection occurs only sporadically during the execution of your program. It will not occur simply because one or more objects exist that are no longer used. For efficiency، the garbage collector will usually run only when two conditions are met: - There are objects to recycle. - There is a reason to recycle them.

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

When is a “this” reference made?

A

When a method is called، it is automatically passed an implicit argument that is a reference to the invoking object (that is، the object on which the method is called). This reference is called this.

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

What is the most prominent use of the “this” keyword?

A

this has some important uses. For example، the Java syntax permits the name of a parameter or a local variable to be the same as the name of an instance variable. When this happens، the local name hides the instance variable. You can gain access to the hidden instance variable by referring to it through this.

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

What are the two basic types of class members and how do they effect the class code?

A

Although Java’s approach is a bit more sophisticated، in essence، there are two basic types of class members: public and private. A public member can be freely accessed by code defined outside of its class. A private member can be accessed only by other methods defined by its class. It is through the use of private members that access is controlled.

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

What type of security does restricting access to a class provide?

A

when correctly implemented، a class creates a “black box” that can be used، but the inner workings of which are not open to tampering.

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

How is member access control achieved?

A

Member access control is achieved through the use of three access modifiers: public، private، and protected. As explained، if no access modifier is used، the default access setting is assumed.

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

What does the private and public specifier mean? What happened when it’s default?

A

When a member of a class is modified by the public specifier، that member can be accessed by any other code in your program. This includes by methods defined inside other classes. When a member of a class is specified as private، that member can be accessed only by other members of its class. Thus، methods in other classes cannot access a private member of another class. The default access setting (in which no access modifier is used) is the same as public unless your program is broken down into packages.

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

Simply put، what are packages?

A

A package is، essentially، a grouping of classes. Packages are both an organizational and an access control feature،

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

What are the two ways in which an argument can be passed to a subroutine?

A

The first way is call-by-value. This approach copies the value of an argument into the formal parameter of the subroutine. Therefore، changes made to the parameter of the subroutine have no effect on the argument in the call. The second way an argument can be passed is call-by-reference. In this approach، a reference to an argument (not the value of the argument) is passed to the parameter. Inside the subroutine، this reference is used to access the actual argument specified in the call. This means that changes made to the parameter will affect the argument used to call the subroutine.

34
Q

When you pass a primitive type value to a method، what way is it passed?

A

When you pass a primitive type، such as int or double، to a method، it is passed by value. Thus، a copy of the argument is made، and what occurs to the parameter that receives the argument has no effect outside the method.

35
Q

What are objects passed to methods as? What type of reference and why does it act that way?

A

When you pass an object to a method، the situation changes dramatically، because objects are implicitly passed by reference. Keep in mind that when you create a variable of a class type، you are creating a reference to an object. It is the reference، not the object itself، that is actually passed to the method. As a result، when you pass this reference to a method، the parameter that receives it will refer to the same object as that referred to by the argument. This effectively means that objects are passed to methods by use of call-by-reference.

36
Q

Is there any way that I can pass a primitive type by reference?

A

Not directly. However، Java defines a set of classes that wrap the primitive types in objects. These are Double، Float، Byte، Short، Integer، Long، and Character. In addition to allowing a primitive type to be passed by reference، these wrapper classes define several methods that enable you to manipulate their values. For example، the numeric type wrappers include methods that convert a numeric value from its binary form into its human-readable String form، and vice versa.

37
Q

Can you return objects of classes?

A

You can، of course، also return objects of classes that you create.

38
Q

When an object is returned by a method، how long does it remain in existence?

A

When an object is returned by a method، it remains in existence until there are no more references to it. At that point، it is subject to garbage collection. Thus، an object won’t be destroyed just because the method that created it terminates.

39
Q

What is method overloading?

A

In Java، two or more methods within the same class can share the same name، as long as their parameter declarations are different. When this is the case، the methods are said to be overloaded، and the process is referred to as method overloading.

40
Q

How do you overload a method?

A

In general، to overload a method، simply declare different versions of it. The compiler takes care of the rest. You must observe one important restriction: the type and/or number of the parameters of each overloaded method must differ. It is not sufficient for two methods to differ only in their return types. (Return types do not provide sufficient information in all cases for Java to decide which method to use.) Of course، overloaded methods may differ in their return types، too.

41
Q

How does method overloading support polymorphism?

A

Method overloading supports polymorphism because it is one way that Java implements the “one interface، multiple methods” paradigm.

42
Q

Can you overload two unrelated methods?

A

When you overload a method، each version of that method can perform any activity you desire. There is no rule stating that overloaded methods must relate to one another. However، from a stylistic point of view، method overloading implies a relationship. Thus، while you can Page 203use the same name to overload unrelated methods، you should not.

43
Q

What is recursion and what is its key component?

A

In Java، a method can call itself. This process is called recursion، and a method that calls itself is said to be recursive. In general، recursion is the process of defining something in terms of itself and is somewhat similar to a circular definition. The key component of a recursive method is a statement that executes a call to itself.

44
Q

— Add screenshot and pink highlights — How would you code a program that uses recursion to find a factorial of a number. For comparison، how would you do the same thing with iterations?

A

The following program shows a recursive way to compute the factorial of a number. For comparison purposes، a nonrecursive equivalent is also included. — — The operation of the nonrecursive method factI( ) should be clear. The operation of the recursive factR( ) is a bit more complex. When factR( ) is called with an argument of 1، the method returns 1; otherwise، it returns the product of factR(n–1)*n. To evaluate this expression، factR( ) is called with n–1. This process repeats until n equals 1 and the calls to the method begin returning.

45
Q

Why can you say that a recursive method telescopes out and back?

A

A recursive call does not make a new copy of the method. Only the arguments are new. As each recursive call returns، the old local variables and parameters are removed from the stack، and execution resumes at the point of the call inside the method. Recursive methods could be said to “telescope” out and back.

46
Q

What is the main advantage to recursion over iterative code?

A

The main advantage to recursion is that some types of algorithms can be implemented more clearly and simply recursively than they can be iteratively.

47
Q

What does the keyword static do? When is the member accessible?

A

Normally a class member must be accessed through an object of its class، but it is possible to create a member that can be used by itself، without reference to a specific instance. To create such a member، precede its declaration with the keyword static. When a member is declared static، it can be accessed before any objects of its class are created، and without reference to any object.

48
Q

How do you use a static member outside the class?

A

Outside the class، to use a static member، you need only specify the name of its class followed by the dot operator.

49
Q

What is the difference between a normal method and a static method?

A

The difference between a static method and a normal method is that the static method is called through its class name، without any object of that class being created.

50
Q

What are the restrictions for static methods?

A

Methods declared as static have several restrictions: ● They can directly call only other static methods in their class. ● They can directly access only static variables in their class. ● They do not have a this reference.

51
Q

Which version of java were nested classes introduced?

A

In Java، you can define a nested class. This is a class that is declared within another class. Frankly، the nested class is a somewhat advanced topic. In fact، nested classes were not even allowed in the first version of Java. It was not until Java 1.1 that they were added.

52
Q

What are the two types of nested classes and what is the accessibility of the basic kind?

A

There are two general types of nested classes: those that are preceded by the static modifier and those that are not. The only type that we are concerned about in this book is the non-static variety. This type of nested class is also called an inner class. It has access to all of the variables and methods of its outer class and may refer to them directly in the same way that other non-static members of the outer class do.

53
Q

What is an anonymous inner class?

A

One last point: You can create an inner class that does not have a name. This is called an anonymous inner class. An object of an anonymous inner class is instantiated when the class is declared، using new.

54
Q

What makes a static nested class different from a non-static one?

A

A static nested class is one that has the static modifier applied. Because it is static، it can access only other static members of the enclosing class directly. It must access other members of its outer class through an object reference.

55
Q

What is a superclass and subclasses relationship?

A

In the language of Java، a class that is inherited is called a superclass. The class that does the inheriting is called a subclass. Therefore، a subclass is a specialized version of a superclass. It inherits all of the variables and methods defined by the superclass and adds its own، unique elements.

56
Q

What does the “extends” keyword mean?

A

Java supports inheritance by allowing one class to incorporate another class into its declaration. This is done by using the extends keyword. Thus، the subclass adds to (extends) the superclass.

57
Q

_As an example of superclass and subclass relationship، how would you code a program that initially created a 2d square and calculated its area? Then it creates a subclass that does the same for a 2d triangle?

A

Here، TwoDShape defines the attributes of a “generic” two-dimensional shape، such as a square، rectangle، triangle، and so on. The Triangle class creates a specific type of TwoDShape، in this case، a triangle. The Triangle class includes all of TwoDObject and adds the field style، the method area( )، and the method showStyle( ). The triangle’s style is stored in style. This can be any string that describes the triangle، such as “filled”، “outlined”، “transparent”، or even something like “warning symbol”، “isosceles”، or “rounded”. The area( ) method computes and returns the area of the triangle، and showStyle( ) displays the triangle style. Because Triangle includes all of the members of its superclass، TwoDShape، it can access width and height inside area( ). Also، inside main( )، objects t1 and t2 can refer to width and height directly، as if they were part of Triangle.

58
Q

How many superclass’s can you have for a subclass?

A

The general form of a class declaration that inherits a superclass is shown here: class subclass-name extends superclass-name { // body of class } You can specify only one superclass for any subclass that you create. Java does not support the inheritance of multiple superclasses into a single subclass. You can، however، create a hierarchy of inheritance in which a subclass becomes a superclass of another subclass. Of course، no class can be a superclass of itself.

59
Q

What type of access does a subclass have to the private field of it’s superclass?

A

As you learned in Chapter 6، often an instance variable of a class will be declared private to prevent its unauthorized use or tampering. Inheriting a class does not overrule the private access restriction. Thus، even though a subclass includes all of the members of its superclass، it cannot access those members of the superclass that have been declared private.

60
Q

When should you make an instance variable private or not? What’s the point of making it private?

A

There are no hard and fast rules، but here are two general principles. If an instance variable is to be used only by methods defined within its class، then it should be made private. If an instance variable must be within certain bounds، then it should be private and made available only through accessor methods. This way، you can prevent invalid values from being assigned.

61
Q

What constructor is responsible for building an object of the subclass?

A

The constructor for the superclass constructs the superclass portion of the object، and the constructor for the subclass constructs the subclass part. This makes sense because the superclass has no knowledge of or access to any element in a subclass. Thus، their construction must be separate

62
Q

What does the keyword “super” do?

A

When both the superclass and the subclass define constructors، the process is a bit more complicated because both the superclass and subclass constructors must be executed. In this case، you must use another of Java’s keywords، super، which has two general forms.

63
Q

What is the form of the “super” keyword?

A

A subclass can call a constructor defined by its superclass by use of the following form of super: super(parameter-list); Here، parameter-list specifies any parameters needed by the constructor in the superclass. super( ) must always be the first statement executed inside a subclass constructor.

64
Q

When calling a super()، which constructor is the super calling?

A

When a subclass calls super( )، it is calling the constructor of its immediate superclass. Thus، super( ) always refers to the superclass Page 242immediately above the calling class.

65
Q

Where must the super() be placed inside a subclass’s constructor?

A

super( ) must always be the first statement executed inside a subclass constructor.

66
Q

What is the form of a “super” that acts like a “this” but is most applicable where members are hidden inside a subclass which have the same name in the superclass?

A

There is a second form of super that acts somewhat like this، except that it always refers to the superclass of the subclass in which it is used. This usage has the following general form: super.member Here، member can be either a method or an instance variable. This form of super is most applicable to situations in which member names of a subclass hide members by the same name in the superclass.

67
Q

What is the inheritance of a subclass twice over? What does the subclass of a subclass inherit?

A

given three classes called A، B، and C، C can be a subclass of B، which is a subclass of A. When this type of situation occurs، each subclass inherits all of the traits found in all of its superclasses. In this case، C inherits all aspects of B and A.

68
Q

Which superclass does the super() refer to?

A

super( ) always refers to the constructor in the closest superclass.

69
Q

Whose constructor executes first: superclass or subclass?

A

in a class hierarchy، constructors complete their execution in order of derivation، from superclass to subclass.

70
Q

How does a superclass get info from its subclass?

A

A reference variable of a superclass can be assigned a reference to an object of any subclass derived from that superclass. In other words، a superclass reference can refer to a subclass object.

71
Q

How would you make a code that demonstrates how a superclass can accept an object from it’s subclass? (As a side not، it makes a reference in the superclass)

A

The key point is that TwoDshape( ) is expecting a TwoDShape object. However، Triangle( ) passes it a Triangle object. The reason this works is because، as explained، a superclass reference can refer to a subclass object. Thus، it is perfectly acceptable to pass TwoDShape( ) a reference to an object of a class derived from TwoDShape.

72
Q

What does it mean to override?

A

In a class hierarchy، when a method in a subclass has the same return type and signature as a method in its superclass، then the method in the subclass is said to override the method in the superclass.

73
Q

What happens if a method isn’t overridden?

A

Method overriding occurs only when the signatures of the two methods are identical. If they are not، then the two methods are simply overloaded.

74
Q

What is an abstract type modifier and what is its general form?

A

An abstract method is created by specifying the abstract type modifier. An abstract method contains no body and is، therefore، not implemented by the superclass. Thus، a subclass must override it—it cannot simply use the version defined in the superclass. To declare an abstract method، use this general form: abstract type name(parameter-list); As you can see، no method body is present. The abstract modifier can be used only on instance methods. It cannot be applied to static methods or to constructors.

75
Q

If you have an abstract type modifier، what must you do to the class? What major type of code is off limits in a class like this?

A

A class that contains one or more abstract methods must also be declared as abstract by preceding its class declaration with the abstract modifier. Since an abstract class does not define a complete implementation، there can be no objects of an abstract class. Thus، attempting to create an object of an abstract class by using new will result in a compile-time error.

76
Q

When may a subclass to an abstract class drop the abstract class specification?

A

When a subclass inherits an abstract class، it must implement all of the abstract methods in the superclass. If it doesn’t، then the subclass must also be specified as abstract. Thus، the abstract attribute is inherited until such time as a complete implementation is achieved.

77
Q

How do you prevent a method from being overridden?

A

To prevent a method from being overridden، specify final as a modifier at the start of its declaration. Methods declared as final cannot be overridden.

78
Q

What does declaring the class as final do? How does that affect the methods?

A

You can prevent a class from being inherited by preceding its declaration with final. Declaring a class as final implicitly declares all of its methods as final، too. As you might expect، it is illegal to declare a class as both abstract and final

79
Q

What does “final” mean?

A

final can also be applied to member variables to create what amounts to named constants. If you precede a class variable’s name with final، its value cannot be changed throughout the lifetime of your program.

80
Q

Why is the common typing style of final consonants?

A

As a point of style، many Java programmers use uppercase identifiers for final constants، as does the preceding example. But this is not a hard and fast rule.

81
Q

Can final member variables be made static? Can final be used on method parameters and local variables?

A

The answer to both is Yes. Making a final member variable static lets you refer to the constant through its class name rather than through an object. Declaring a parameter final prevents it from being changed within the method. Declaring a local variable final prevents it from being assigned a value more than once.