SCJP Chapter 2

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Static methods can't be ____ , but they can be ____ .

Static methods can't be overridden, but they can be redefined.

A constructor ____ be directly invoked only by another constructor

A constructor can be directly invoked only by another constructor (using a call to super()or this()).

A ____ is always invoked when a new ____ is created.

A constructor is always invoked when a new object is created. Without the "new" keyword, we simply have a declaration of a variable Obwob j; //instead of a new object being created /a class being instantiated Obwob j = new Obwob; //a new object has been created.

A reference variable is always of a single, ____ type, but it can refer to a ____ object.

A reference variable is always of a single, unchangeable type, but it can refer to a subtype object.

A single class can implement ____ interfaces.

A single class can implement many interfaces.

A single object can be referred to by reference variables of many different types — as long as they are the same type or a ____ of the object.

A single object can be referred to by reference variables of many different types —as long as they are the same type or a supertype of the object.

A static method ____ access an instance variable directly.

A staticmethod can't access an instance variable directly. But it can access it by 1) creating an instance of the class that contains the instance variable 2) then using the . operator (ClasswithVar.whatevervar) to access the variable. Two examples: public class CarClass { public String carName; public static void main(String[] args) { // Create instance of enclosing class and save it in // a reference, viz, the local variable "carClass". CarClass carClass = new CarClass(); // Use this instance variable to access // the member variable "carName". carClass.carName = "Mazda"; //carName = "Mazda"; // cannot access static member here } public void setterMethod() { carName = "Mazda"; // works fine } } __________________________ public class Program { private int count; public Program(int ballcount){ count=ballcount; } public static void main(String argv[]){ Program s = new Program(99); //System.out.println(count); //compile time error //add(10); //compile time error System.out.println(s.count); s.add(10); System.out.println(s.count); } private void add(int num) { count += num; } } The output result is 99 109

A subclass uses ____() to call the superclass version of an overridden method.

A subclass uses super.overriddenMethodName()to call the superclass version of an overridden method.

____ classes have constructors that are ____ when a concrete subclass is instantiated.

Abstract classes have constructors that are called when a concrete subclass is instantiated.

Abstract methods must be overridden by the first ____ (____) subclass.

Abstract methods must be overridden by the first concrete (non-abstract) subclass.

____ classes (except class Object), are subclasses of type Object, and therefore they ____ Object's ____.

All classes (except class Object), are subclasses of type Object, and therefore they inherit Object's methods.

All static members belong to the ____ , not to any ____ .

All static members belong to the class, not to any instance.

An array is a ____ return type, both to ____ and ____ as a value.

An array is a legal return type, both to declare and return as a value.

Calls to this() and super() ____ be in the same constructor.

Calls to this()and super()cannot be in the same constructor. You can have one or the other, but never both.

What is cohesion?

Cohesion refers to the degree in which a class has a single, well-defined role or responsibility.

Constructors are never ____ , thus they cannot be ____ .

Constructors are never inherited, thus they cannot be overridden.

Constructors can call ____ can call ____ , and so on, but sooner or later one of them better call ____ ()or the stack will ____ .

Constructors can call constructors can call constructors, and so on, but sooner or later one of them better call super()or the stack will explode.

Which access modifiers can constructors use?

Constructors can use any access modifier (even private!). If another class cannot call a MyClass constructor, it cannot directly create MyClass objects. (A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.) Strictly speaking a class with a private constructor can be subclassed as long as the constructor is accessible from the subclass's constructor, as is for example the case when the subclass is a nested class. - Let's say I want to restrict the access to the constructor of a public class, so that only certain classes may create an instance of it, but all classes are able to use the instance. In Java, I know two ways to archive this: Make a public static inner class with a private constructor and instantiate it in the outer class. Make the classes constructor package-private (restricts access to all classes in the same package).

Constructors ____ have a return type.

Constructors don't have a return type. If you see code with a return type, it's a method with the same name as the class, it's not a constructor.

Constructors ____ have the same name as the class.

Constructors must have the same name as the class.

What is coupling?

Coupling refers to the degree to which one class knows about or uses members of another class.

Downcasting: If you have a reference variable that refers to a ____ object, you can assign it to a reference variable of the ____. You ____ make an ____ cast to do this, and the result is that you can access the subtype's members with this new reference variable.

Downcasting: If you have a reference variable that refers to a subtype object, you can assign it to a reference variable of the subtype. You must make an explicit cast to do this, and the result is that you can access the subtype's members with this new reference variable.

Each ____ in an object's ____ ____ will have a constructor called.

Each superclass in an object's inheritance tree will have a constructor called.

❑Encapsulated code has two features: ❑Instance variables are kept ____ (usually with the ____ modifier). ❑____ and ____ methods provide access to ____ variables.

Encapsulated code has two features: ❑Instance variables are kept protected (usually with the private modifier). ❑Getter and setter methods provide access to instance variables. Encapsulation, in my opinion, is strongly related to data abstraction. They both form a mechanism like a Black Box (airplanes` black boxes) or a clock - you just see the display, but you don't have an idea what's hidden inside and how it works. And you don't care. All data and operations are collected in your class. Good practice is hiding the details: all fields private and usage of getters and setters to gain access to your fields. That's how you can implement complex logic in your getters/setters, you can restrict access (read-only, write-only or both, or none). Finally, you show users only the information that he/she needs. Remember the example with the clock- you don't mind if the mechanism gets replaced, if you see this display, working the same way. Make private fields, public/protected getters and setters; private methods for internal algorithms that are no interest for the user; public methods that user can use. If you update your app and change your logic, it should be invisible for the user- he just have to use the same algorithm and a set of operations.

Encapsulation helps hide ____ behind an ____ (or ____).

Encapsulation helps hide implementation behind an interface (or API). API stand for Application Programming Interface.

Every class, even an ____ class, has at ____ one ____.

Every class, even an abstract class, has at least one constructor.

True or False: Interfaces have constructors.

False: Interfaces do not have constructors.

For methods with primitive return types, any value that can be ____ ____ to the return type can be returned.

For methods with primitive return types, any value that can be implicitly converted to the return type can be returned. //for example : public int foo () { float f = 32.5f; return (int) f; }

HAS-A means an instance of one class "has a" ____ ____ an instance of another class or another instance of the ____ class.

HAS-A means an instance of one class "has a" reference to an instance of another class or another instance of the same class.

____ cohesion is the desirable state of a class whose members support a single, well-focused role or responsibility.

High cohesion is the desirable state of a class whose members support a single, well-focused role or responsibility. High Cohesion - that's the entity of a class or method. You shouldn't develop a method that acts like a whole big application (for example, all of your program in a method). When an exception comes out, it's harder to find the problem and look over thousands of lines code. When you keep strong cohesion, your methods act like 1 simple organism and it's easier to look over them. High cohesion: System.out.println(message); Simple operation, gets the information and send it to the console.

IS-A is expressed with the keyword ____.

IS-A is expressed with the keyword extends.

IS-A refers to ____ or ____.

IS-A refers to inheritance or implementation.

IS-A, "____ ____," and "is a ____ ____ " are all equivalent expressions.

IS-A, "inherits from," and "is a subtype of " are all equivalent expressions.

What must you do if your superclass does not have a no-arg constructor?

If your superclass does not have a no-arg constructor, you must create a constructor and insert a call to super()with arguments matching those of the superclass constructor.

Inheritance allows a class to be a subclass of a superclass, and thereby inherit ____ and ____ variables and methods of the superclass.

Inheritance allows a class to be a subclass of a superclass, and thereby inherit public and protected variables and methods of the superclass.

Inheritance is a key concept that underlies IS-A, ____, overriding, ____, and ____.

Inheritance is a key concept that underlies IS-A, polymorphism, overriding, overloading, and casting.

Instance members are accessible ____ ____ the super constructor runs.

Instance members are accessible only after the super constructor runs.

Issues with calls to this() ❑May appear ____ as the first statement in a ____ . ❑The ____ list determines which overloaded constructor is called.

Issues with calls to this() May appear only as the first statement in a constructor. ❑The argument list determines which overloaded constructor is called.

Loose coupling is the ____ state of having classes that are well ____ , ____ references to each other, and ____ the breadth of ____ usage.

Loose coupling is the desirable state of having classes that are well encapsulated, minimize references to each other, and limit the breadth of API usage (best example I've found so far was calling the API to authenticate data; the more calls, the slower the program and the more dependent the program would be on the API itself and the network connection)

____ cohesion is the undesirable state of a class whose members support multiple, unfocused roles or responsibilities.

Low cohesion is the undesirable state of a class whose members support multiple, unfocused roles or responsibilities. Low cohesion: a method that executes connecting to Internet, downloading information, putting it into a database, changing details, exporting, printing, uploading back to Internet. All of this activities in a method. How many errors could occur here? ;)

____ can be overridden or overloaded; ____ can be overloaded but not overridden.

Methods can be overridden or overloaded; constructors can be overloaded but not overridden.

Methods from a superclass ____ be overloaded in a subclass.

Methods from a superclass can be overloaded in a subclass.

Methods with an interface return type, can return any ____.

Methods with an interface return type, can return any implementer. ... public interface Chewable { } public class Gum implements Chewable { } public class TestChewable { // Method with an interface return type public Chewable getChewable() { return new Gum(); // Return interface implementer } }

Methods with an object reference return type, can return a ____.

Methods with an object reference return type, can return a subtype.

____ can be returned from a void, but you ____ return nothing. You ____ return nothing from a method with a non-void return type.

NOTHING can be returned from a void, but you can return nothing. You're allowed to simply say return, in any method with a voidreturn type, to bust out of a method early. You CAN'T return nothing from a method with a non-void return type.

Object reference return types ____ accept null as a return value.

Object reference return types can accept nullas a return value.

____ type determines which overridden method is used at RUNTIME.

Object type (not the reference variable's type), determines which overridden method is used at runtime.

Only ____ methods may be overridden, and remember that ____ methods are not inherited.

Only inherited methods may be overridden, and remember that private methods are not inherited.

❑Overloaded methods ❑Must have ____ ____ lists ❑May have ____ ____ types, if argument lists are ____ different ❑____ have different access modifiers ❑____ throw different exceptions

Overloaded methods ❑Must have different argument lists ❑May have different return types, if argument lists are also different ❑May have different access modifiers ❑May throw different exceptions

Overloaded methods ____ change return types; overridden methods ____, except in the case of ____ returns.

Overloaded methods can change return types; overridden methods cannot, except in the case of covariant returns.

____ means reusing a method name, but with different arguments.

Overloading means reusing a method name, but with different arguments.

Polymorphic method invocations apply only to ____ instance methods.

Polymorphic method invocations apply only to overridden instance methods.

Polymorphism applies to ____, not to ____.

Polymorphism applies to overriding, not to overloading.

Polymorphism means "____ ____."

Polymorphism means "many forms."

____ type determines which overloaded method will be used at COMPILE time.

Reference type determines which overloaded method will be used at compile time.

Remember that using a ____ variable with the dot operator is really a ____ ____ , and the compiler will substitute the ____ name for the ____ variable

Remember that using a reference variable with the dot operator is really a syntax trick, and the com-piler will substitute the class name for the reference variable, for instance: d.doStuff(); becomes: Dog.doStuff();

The compiler ____ add a call to super()unless you have already put in a call to this() or super().

The compiler will add a call to super()unless you have already put in a call to this()or super().

The compiler ____ create a default constructor if you don't create any constructors in your class.

The compiler will create a default constructor if you don't create any construc-tors in your class.

The default constructor is a ____-____ constructor with a ____-____ call to ____().

The default constructor is a no-arg constructor with a no-arg call to super().

The first statement of every constructor ____ be a call to either this()(an ____ constructor) or ____().

The first statement of every constructor must be a call to either this()(an overloaded constructor) or super(). public class Simple { public Simple() { this(null); } public Simple(Resource r) { this(r, null); } public Simple(Resource r1, Resource r2) { // Guard statements, initialize resources or throw exceptions if // the resources are wrong if (r1 == null) { r1 = new Resource(); } if (r2 == null) { r2 = new Resource(); } // do whatever with resources } } Default parameters. It's very common for a constructor with fewer parameters to call a constructor with more parameters, supplying default values. For this usage, the constructors with fewer parameters will frequently consist of only the "this" call. "this". Instead of calling the constructor with the class name, use the keyword this. The compiler matches "this" with a constructor with the appropriate number and types of parameters, and calls it. Must be first. The "this" call must be the very first line of the constructor. // File : oop/timeofday/TimeOfDay1d.java // Purpose: A time-of-day class with overloaded constructors. // One constructor calls the other using "this". // Author : Fred Swartz, 2007-02-27, Placed in public domain. public class TimeOfDay1d { public int hour; public int minute; //==================================================== constructor public TimeOfDay1d(int h, int m) { hour = h; minute = m; } //==================================================== constructor public TimeOfDay1d(int h) { this(h, 0); // Call other constructor. } }

In polymorphism , the ____ typedetermines which method will be used.

The object type (on the right).

In polymorphism, the ____ type determines which variable will be used.

The reference type (of the left). If the class being used as the reference type doesn't have the variable, the compiler will complain that it can't find the symbol. Here's an example clarifying the last three questions: class Animal { public String fur = "maybe"; public void makeNoise() { System.out.println("Generic animal noise"); } } class Dog extends Animal { public String fur = "yes"; public void makeNoise() { System.out.println("bark bark"); } public void wagTail() { System.out.println("left right..."); } } public class AnimalTester { public static void main(String[] args) { Animal a = new Dog(); //polymorphic reference i.e. supertype reference refers to a subtype object //a.wagTail(); //The reference variables type ( not the object's type) determines which methods can be called //Since the reference is of type "Animal", it DOES NOT know about the wagTail() method of dog. //So you cannot call the wagTail() method. This will lead to a compile time error. Hence I have commented it out :) a.makeNoise(); //Object type ( not the reference variables type) determines which overridden method is used at runtime. //Here even though the reference type is of type "Animal", during run-time the Dog's makeNoise() method is called //You can verify this by compiling and running the code. It will print "bark bark" and not "Generic animal noise" System.out.println(a.fur); // the reference variable type will be used here. The output for this program is "bark bark" and "maybe". } }

The ____ type determines which methods can be used polymorphically

The reference type (on the left) determines this.

The ____ variable's type determines which methods can be called!

The reference variable's type (not the object's type), determines which methods can be called!

There are two types of reference variable casting: ____ and ____.

There are two types of reference variable casting: downcasting and upcasting.

____ coupling is the undesirable state of having classes that break the rules of loose coupling.

Tight coupling is the undesirable state of having classes that break the rules of loose coupling.

Describe the two major steps in constructor execution

Typical constructor execution occurs as follows: ❑The constructor calls its superclass constructor, which calls its superclass constructor, and so on all the way up to the Object constructor. ❑The Object constructor executes and then returns to the calling constructor, which runs to completion and then returns to its calling constructor, and so on back down to the completion of the constructor ofthe actual instance being created.

Upcasting: You can assign a reference variable to a ____ reference variable ____ or ____. This is an inherently ____ operation because the assignment restricts the access capabilities of the new variable.

Upcasting: You can assign a reference variable to a supertype reference vari-able explicitly or implicitly. This is an inherently safe operation because the assignment restricts the access capabilities of the new variable.

Use ____ methods to implement behaviors that are not ____ by the state of any ____ .

Use static methods to implement behaviors that are not affected by the state of any instances.

Use static ____ to hold data that is class specific as opposed to instance specific—there will be ____ ____ copy of a static variable.

Use static variables to hold data that is class specific as opposed to instance specific—there will be only one copy of a static variable.

Use the ____ operator to access static members

Use the dot operator to access staticmembers

When you implement an interface, you are ____ its ____.

When you implement an interface, you are fulfilling its contract.

Here are two points from this Chapter that need to be hammered in: 1) The reference variable's type (not the object's type), determines which methods can be called! 2) Object type determines which overridden method is used at RUNTIME. What's going on?

With polymorphism, the reference variable type determines whether or not a particular method *can possibly* be used (left) But the object type will determine the method that * will * be used. (right) Also, if a variable is called, the reference type will be used (left).

With respect to the method it overrides, the overriding method ❑Must have the same ____ list. ❑Must have the same ____ type ❑Must not have a ____ ____ access modifier. ❑May have a ____ ____ access modifier. ❑Must not throw new or broader ____ exceptions. ❑May throw ____ or ____ checked exceptions, or any ____ exception.

With respect to the method it overrides, the overriding method ❑Must have the same argument list. ❑Must have the same return type, except that as of Java 5, the return type can be a subclass—this is known as a covariant return. ❑Must not have a more restrictive access modifier. ❑May have a less restrictive access modifier. ❑Must not throw new or broader checked exceptions. ❑May throw fewer or narrower checked exceptions, or any unchecked exception.

You implement an interface by properly and concretely ____ ____ of the methods defined by the interface.

You implement an interface by properly and concretely overriding all of the methods defined by the interface.

____ methods cannot be overridden.

final methods cannot be overridden.

int or smaller expressions always result in an ____

int or _____ expressions always result in an int. If we have a section of code where: byte a = 3; byte y = 6; //and we want to add these two byte z= a+y; //this will not work without an explicit cast. The compiler will yack about possible loss of precision. It expects an int and if we want to get back something other than an int we must explicitly state this. In this case, it would be: byte z = (byte) (a+y);


संबंधित स्टडी सेट्स

Chapter 11 - Health Information Systems

View Set

Nursing Sciences EAQ developmental theories

View Set

7. Small Business Strategies Entrepreneurship

View Set

Mental Health Exam 1 - Study Guide

View Set

Call of the Wild Seven Major Themes

View Set

Media Literacy: Chapter 11 and Chapter 12

View Set