Lesson 3.4: Object-Oriented Programming Flashcards

1
Q

Why are primitive types used over objects?

A

Primitive types، rather than objects، are used for these quantities for the sake of performance. Using objects for these basic types would add an unacceptable overhead to even the simplest of calculations. Thus، the primitive types are not part of the object hierarchy، and they do not inherit Object.

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

In layman’s terms، what are type wrappers?

A

type wrappers، which are classes that encapsulate a primitive type within an object.

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

What are the 8 type wrappers and what are they packaged in?

A

The type wrappers are Double، Float، Long، Integer، Short، Byte، Character، and Boolean، which are packaged in java.lang. These classes offer a wide array of methods that allow you to fully integrate the primitive types into Java’s object hierarchy.

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

What are the most used type wrappers and what abstract class do they inherit?

A

437Probably the most commonly used type wrappers are those that represent numeric values. These are Byte، Short، Integer، Long، Float، and Double. All of the numeric type wrappers inherit the abstract class Number.

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

What does the abstract class “Number” declare? What is the general form for its methods?

A

Number declares methods that return the value of an object in each of the different numeric types. These methods are shown here: byte byteValue( ) double doubleValue( ) float floatValue( ) int intValue( ) long longValue( ) short shortValue( )

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

Though not used anymore، what are the general forms for the integer and double wrapper constructors? Are there any other wrapper types that get a constructor?

A

All of the numeric type wrappers define constructors that allow an object to be constructed from a given value، or a string representation of that value. For example، here are the constructors defined for Integer and Double: Integer(int num) Integer(String str) throws NumberFormatException Double(double num) Double(String str) throws NumberFormatException If str does not contain a valid numeric value، then a NumberFormatException is thrown. However، beginning with JDK 9، the type-wrapper constructors have been deprecated.

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

Why is using a type constructor outdated for obtaining a wrapper object? What should you do to obtain a wrapper object? What is it’s general form?

A

Today، it is recommended that you use one of the valueOf( ) methods to obtain a wrapper object. The valueOf( ) method is a static member of all of the wrapper classes and all numeric classes support forms that convert a numeric value or a string into an object. For example، here are two forms supported by Integer: static Integer valueOf(int val) static Integer valueOf(String valStr) throws NumberFormatException Here، val specifies an integer value and valStr specifies a string that represents a properly formatted numeric value in string form.

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

What is boxing and in what version did doing it manually become outdated?

A

The process of encapsulating a value within an object is called boxing. Prior to JDK 5، all boxing took place manually، with the programmer explicitly constructing an instance of a wrapper with the desired value

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

What is unboxing?

A

The process of extracting a value from a type wrapper is called unboxing. Again، prior to JDK 5، all unboxing also took place manually، with the programmer explicitly calling a method on the wrapper to obtain its value.

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

What is autoboxing and auto-unboxing?

A

Autoboxing is the process by which a primitive type is automatically encapsulated (boxed) into its equivalent type wrapper whenever an object of that type is needed. There is no need to explicitly obtain an object. Auto-unboxing is the process by which the value of a boxed object is automatically extracted (unboxed) from a type wrapper when its value is needed.

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

How do you autobox a value?

A

You need only assign that value to a type-wrapper reference. Java automatically constructs the object for you. For example، here is the modern way to declare an Integer object that has the value 100: Integer iOb = 100; // autobox an int Notice that the object is not explicitly boxed. Java handles this for you، automatically.

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

How do you auto-unbox a value?

A

To unbox an object، simply assign that object reference to a primitive-type variable. For example، to unbox iOb، you can use this line: int i = iOb; // auto-unbox Java handles the details for you.

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

Give me an example of autoboxing/unboxing in a piece of code.

A

For example، consider the following program: Code to the right >

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

Why are primitive types used instead of autoboxing & unboxing?

A

. It is far less efficient than the equivalent code written using the primitive type double. The reason is that each autobox and auto-unbox adds overhead that is not present if the primitive type is used.

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

Why are parameterized types important? What is its relationship with generics?

A

At its core، the term generics means parameterized types. Parameterized types are important because they enable you to create classes، interfaces، and methods in which the type of data Page 453upon which they operate is specified as a parameter. A class، interface، or method that operates on a type parameter is called generic، as in generic class or generic method.

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

What is similar in Java to C++’s “templates”?

A

Java generics are similar to templates in C++. What Java calls a parameterized type، C++ calls a template. However، Java generics and C++ templates are not the same، and there are some fundamental differences between the two approaches to generic types. For the most part، Java’s approach is simpler to use. A word of warning: If you have a background in C++، it is important not to jump to conclusions about how generics work in Java. The two approaches to generic code differ in subtle but fundamental ways.

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

What is similar in Java to C++’s “templates”?

A

Java generics are similar to templates in C++. What Java calls a parameterized type، C++ calls a template. However، Java generics and C++ templates are not the same، and there are some fundamental differences between the two approaches to generic types. For the most part، Java’s approach is simpler to use. A word of warning: If you have a background in C++، it is important not to jump to conclusions about how generics work in Java. The two approaches to generic code differ in subtle but fundamental ways.

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

What was the problem that created the need for generics in Java

A

Because Object is the superclass of all other classes، an Object reference can refer to any type of object. Thus، in pre-generics code، generalized classes، interfaces، and methods used Object references to operate on various types of data. The problem was that they could not do so with type safety because casts were needed to explicitly convert from Object to the actual type of data being operated upon. Thus، it was possible to accidentally create type mismatches.

19
Q

Give me an example of a “generic class”.

A

The following program defines two classes. The first is the generic class Gen، and the second is GenDemo، which uses Gen. Code example to the right > Here، T is the name of a type parameter. This name is used as a placeholder for the actual type that will be passed to Gen when an object is created. Notice that T is contained within < >. This syntax can be generalized. Whenever a type parameter is being declared، it is specified within angle brackets. In the declaration of Gen، there is no special significance to the name T. Any valid identifier could have been used، but T is traditional. it is recommended that type parameter names be single-character، capital letters.

20
Q

Give me a code fragment of a type argument. What do type arguments do?

A

Gen iOb; the type Integer is specified within the angle brackets after Gen. In this case، Integer is a type argument that is passed to Gen’s type parameter، T. This effectively creates a version of Gen in which all references to T are translated into references to Integer.

21
Q

What is the process of removing generic type information called?

A

The process of removing generic type information is called erasure

22
Q

What will happen if the type argument doesn’t match match the constructor type?

A

iOb = new Gen(88); when the Gen constructor is called، the type argument Integer is also specified. This is because the type of the object (in this case iOb) to which the reference is being assigned is of type Gen. Thus، the reference returned by new must also be of type Gen. If it isn’t، a compile-time error will result.

23
Q

When declaring an instance of a generic type، what types can you not use?

A

When declaring an instance of a generic type، the type argument passed to the type parameter must be a reference type. You cannot use a primitive type، such as int or char.

24
Q

Why is not being able to use primitive types not a restriction when declaring an instance of a generic type?

A

Of course، not being able to specify a primitive type is not a serious restriction because you can use the type wrappers (as the preceding example did) to encapsulate a primitive type. Further، Java’s autoboxing and auto-unboxing mechanism makes the use of the type wrapper transparent.

25
Q

How can you make a version of a generic constructor that conceptually has a different type?

A

Gen strOb = new Gen(“Generics Test”); Because the type argument is String، String is substituted for T inside Gen. This creates (conceptually) a String version of Gen، as the remaining lines in the program demonstrate.

26
Q

What is the syntax for declaring a generic class?

A

Here is the syntax for declaring a generic class: class class-name { // …

27
Q

What do bound types do?

A

you need some way to tell the compiler that you intend to pass only numeric types to T. Furthermore، you need some way to ensure that only numeric types are actually passed. To handle such situations، Java provides bounded types. When specifying a type parameter، you can create an upper bound that declares the superclass from which all type arguments must be derived.

28
Q

How do you declare a bounded type?

A

through the use of an extends clause when specifying the type parameter، as shown here: This specifies that T can be replaced only by superclass، or subclasses of superclass. Thus، superclass defines an inclusive، upper limit.

29
Q

Give me a code that accepts multiple type parameters in a generic.

A

You can declare more than one type parameter in a generic type. To specify two or more type parameters، simply use a comma-separated list. For example، the following TwoGen class is a variation of the Gen class that has two type parameters: Code example to the right >

30
Q

What is a wildcard argument?

A

The wildcard argument is specified by the ?، and it represents an unknown type.

31
Q

What is the full syntax for declaring a reference to a generic class and creating a generic instance?

A

Here is the full syntax for declaring a reference to a generic class and creating a generic instance: class-name var-name = new class-name(cons-arg-list);

32
Q

Give me an example of a code that uses bounded types.

A

You can use an upper bound to fix the NumericFns class shown earlier by specifying Number as an upper bound، as shown here: Code example to the right >

33
Q

Give me a code fragment that uses a wildcard argument to determine if two generic objects are the same.

A

Using a wildcard، here is one way to write the absEqual( ) method: Code example to the right>

34
Q

How do you declare an upper bound for a wildcard?

A

In general، to establish an upper bound for a wildcard، use the following type of wildcard expression: extends superclass>

35
Q

How do you declare a lower bound for a wildcard?

A

You can also specify a lower bound for a wildcard by adding a super clause to a wildcard declaration. Here is its general form: super subclass> In this case، only classes that are superclasses of subclass are acceptable arguments. This is an inclusive clause.

36
Q

In relation to the return type، where is the type parameter declared?

A

The type parameters are declared before the return type of the method.

37
Q

What does the Comparable interface do?

A

Comparable is an interface declared in java.lang. A class that implements Comparable defines objects that can be ordered. Comparable is generic، and its type parameter specifies the type of objects that it compares.

38
Q

How do wildcards affect what type of objects can be created?

A

One last point: It is important to understand that the wildcard does not affect what type of NumericFns objects can be created. This is governed by the extends clause in the NumericFns declaration. The wildcard simply matches any valid NumericFns object.

39
Q

Give me an example of a very basic generic class.

A

the following very simple generic class: Code example to the right > Gen takes one type parameter، which specifies the type of object stored in ob. Because T is unbounded، the type of T is unrestricted. That is، T can be of any class type.

40
Q

What is the syntax for a generic method?

A

Here is the syntax for a generic method: ret-type meth-name(param-list) { // … In all cases، type-param-list is a comma-separated list of type parameters.

41
Q

If a class is not generic، can constructor in the class be generic?

A

A constructor can be generic، even if its class is not.

42
Q

Give me a basic code example that uses a generic constructor but the class in not generic.

A

For example، in the following program، the class Summation is not generic، but its constructor is. Code example to the right >

43
Q

If generic interfaces are specified like generic classes، give me a code example that uses a generic interface.

A

Generic interfaces are specified just like generic classes. Here is an example. It creates an interface called Containment، which can be implemented by classes that store one or more values. It declares a method called contains( ) that determines if a specified value is contained by the invoking object. Code example to the right >

44
Q

What is the syntax for a generic interface?

A

Once the type parameter has been established، it is simply passed to the interface without further modification. Here is the generalized syntax for a generic interface: interface interface-name { // … Here، type-param-list is a comma-separated list of type parameters. When a generic interface is implemented، you must specify the type arguments، as shown here: