Unit 4 - Object Oriented Programming and Re-usability

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

How can packages be used to mimic conditional compilation? (See TIJ page 220.)

- conditional compilation - allows you to change a switch and get different behavior without changing any other code - changing the package that's imported in order to change the code used in your program from the debug version to the production version

How does protected work with inheritance? (See TIJ pages 258 to 259.)

- protected keyword is a nod to pragmatism - "This is private as far as the class user is concerned, but available to anyone who inherits from this class or anyone else in the same package."

What are the four levels of access specification in Java? (See TIJ page 210.)

- public - protected - package access (no keyword) - private

What is the convention for ordering variables by access level? (See TIJ pages 228 to 229.)

- public , protected, package access, private

What is RTTI and how does it work with downcasting? (See TIJ pages 308 to 310.)

- runtime type information RTTI - act of checking types at runtime - RTTI checks types in downcasting ensure it is safe

What is a compilation unit in Java? (See TIJ page 211.)

- source code file for Java - name ending in .java, public class same as unit name, only 1 public class in unit

What is the format of a Java interface? (See TIJ pages 316 to 317.)

- to create an interface, use the interface keyword instead of the class keyword - as with a class, you can add the public keyword before the interface keyword (but only if that interface is defined in a file of the same name) - if you leave off the public keyword, you get package access, so the interface is only usable within the same package. An interface can also contain fields, but these are implicitly static and final

Why should one be cautious in using final method? (See page 270 of TIJ.)

- to put a "lock" on the method to prevent any inheriting class from changing its meaning, essentially it cannot be overridden in a subclass - one should be cautious using the final method because you never know when a class can be re-used

How is upcasting safe and downcasting not safe with extension? (See TIJ page 308.)

- upcast always safe because base class have bigger interface than derived class

What is the advantage of upcasting? (See TIJ pages 260 to 261.)

- upcasting always safe since going from specific type to general type - can lose methods not gain methods

What access do inner classes have to the containing class variables and methods? (See TIJ pages 347 to 349.)

- when create inner class, object of that inner class has linking to enclosing made it - inner classes have access to right to all elements in enclosing class

What is the pitfall of Java's collision detection? How does Java handle collisions between classes in packages? (See TIJ page 217.)

- when importing libraries, sometimes classes have the same name - everything is fine if you don't write code that actually causes the error - if collision occurs, compiler will force to explicitly choose which class to use

What does the keyword extends accomplish? (See TIJ pages 241 to 243.)

- when inheriting, state before body class - eg. class name extends base class {} - after, class gets all fields and methods in base class

What is the effect of final on objects? (See page 263 to 265 of TIJ.)

- with an object reference, final makes the reference a constant - once the reference is initialized to an object, it can never be changed to point to another object - however, the object itself can be modified

Can a class implement multiple interfaces? (See TIJ pages 330 to 331.)

- yes

Can an interface be nested within another interface? (See TIJ page 339.)

- yes - however, the rules about interfaces—in particular, that all interface elements must be public—are strictly enforced here, so an interface nested within another interface is automatically public and cannot be made private

How does a final argument function? (See pages 266 to 268 of TIJ.)

- you can read the argument, but you can't change it - preventing you from changing what argument reference points to inside a method

What are two advantages of inner classes for control frameworks? (See TIJ page 377.)

1. entire implementation of control framework created in single class, inner classes used many different action() necessary 2. inner classes keep this implementation from becoming awkward, since you're able to easily access any of the members in the outer class

What are two ways of setting a final primitive value? (See page 262 of TIJ.)

1. it can be a compile-time constant that won't ever change. 2. it can be a value initialized at run time that you don't want changed.

What are the constraints on using the public access level? (See TIJ pages 229 to 230.)

1. only one public class per compilation unit 2. name of public class must exactly match name of file containing compilation unit 3. using instance initialization

How does CLASSPATH work with packages? (See TIJ pages 214 to 215.)

- Java interpreter finds environment variable CLASSPATTH, contains 1 or more directories used as roots search for .class files - interpreter will take package name and replace each dot with backslash - then concatenated to various entries in CLASSPATH

What is a good rule for using polymorphic methods within a constructor? (See TIJ pages 301 to 303.)

- a good rule is to do as little as possible to set the object in a good state, and if you can avoid it, don't call any other methods in this class - only safe methods to call inside a constructor class are those that are final in the base class. This also applies to private since they are implicitly final

What are abstract classes and abstract methods? (See TIJ pages 311 to 312.)

- abstract classes and methods only have declaration no body

What is one common use of interface? (See TIJ page 332.)

- aforementioned Strategy design pattern - write a method that performs certain operations, and that method takes an interface that you also specify - you're basically saying, "You can use my method with any object you like, as long as your object conforms to my interface." - this makes your method more flexible, general and reusable

What is package access? (See TIJ pages 221 to 222.)

- all other classes in current have access to that package

What is the package caveat? (See TIJ page 220.)

- anytime you create a package, you implicitly specify a directory structure when you give the package a name. - package must live in the directory indicated by its name, which must be a directory that is searchable starting from the CLASSPATH - note that compiled code is often placed in a different directory than source code, but the path to the compiled code must still be found

What is a good rule for deciding between inheritance and composition? (See TIJ page 261 to 262.)

- asking if you need to upcast from new class to base class - if yes, inheritance necessary

What is public access? (See TIJ pages 222 to 224.)

- available to everyone who uses library

How is multiple inheritance dealt with by interfaces? (See TIJ pages 326 to 327.)

- because an interface has no implementation at all—that is, there is no storage associated with an interface—there's nothing to prevent many interfaces from being combined

What is the advantage of using a blank final? (See page 265 to 266 of TIJ.)

- blank finals provide much more flexibility in the use of the final keyword since - for example, a final field inside a class can now be different for each object, and yet it retains its immutable quality

What is the advantage of upcasting to an interface with inner classes? (See TIJ pages 352 to 353.)

- can then be unseen and unavailable, which is convenient for hiding the implementation

What is an inner class? (See TIJ page 345 to 346.)

- class definition within another class definition

What is an application framework? (See TIJ page 375.)

- class or set of classes designed to solve particular type of problem, takes all complexities of interfacing if operating system and simplifies then for you

What is an anonymous class? (See TIJ page 357.)

- class without name only single object created, useful making instance of object with "extra"

When is composition used compared to when inheritance is used? (See TIJ pages 256 to 258.)

- composition generally used when want functionality of existing class inside new class but not its interface

What advantage does composition have over inheritance? (See TIJ pages 304 to 306.)

- composition more flexible than inheritance - can change implementation at run time - less classes - no conflict between method names

What is the order of constructor calls for base and derived classes? (See TIJ pages 293 to 295.)

- constructor for the base class is always called during the construction process for a derived class. - this call automatically moves up the inheritance hierarchy so that a constructor for every base class is called 1. base-class constructor is called. This step is repeated recursively such that the root of the hierarchy is constructed first, followed by the next-derived class, etc., until the most-derived class is reached. 2. member initializers are called in the order of declaration. 3. body of the derived-class constructor is called

What is composition? (See TIJ page 237.)

- create objects of existing class inside a new class

What is protected baccess? (See TIJ pages 225 to 227.)

- declared in superclass, only accessed by sub classes in other packages or any other class within package

What is delegation in relation to composition and inheritance? (See TIJ pages 246 to 248.)

- delegation midway between inheritance and composition - place member object in class building but at the same time expose all methods from member object in new class

What are characteristics of static inner classes? (See TIJ pages 364 to 365.)

- don't need outer class object in order to create object of nested class - can't access non-static outer class object from object of nested class

What is the one compelling reason for using an inner class? (See TIJ page 369.)

- each inner class independently inherit from implementation - inner class not limited by whether outer class already inheriting from implementation

How does extend differ for interfaces? (See TIJ pages 329 to 330.)

- extends can refer to multiple base interfaces when building a new interface

What is the difference between static final versus final? (See pages 262 to 263 of TIJ.)

- field that is static final has only one piece of storage that cannot be changed.

What is the naming convention for packages? (See TIJ pages 214 to 215.)

- first part of package name is reversed Internet domain name of create of class - second part of resolve package name into directory on machine

How can you use static inner classes for testing? (See TIJ pages 366 to 367.)

- have to have a reference to the other outer class as well - eg. Inner inner = new MyClass().new Inner(); - if Inner was static then it would be - eg. Inner inner = new MyClass.Inner();

How does a method in a derived class affect a method in a base class with the same name but a different signature? (See TIJ pages 255 to 256.)

- if Java base class has method name overloaded several times, redefining that method name in derived class will not hide any of the base class versions

What are the meanings of .this and .new</> in the context of an inner class? (See TIJ pages 350 to 351.)

- if you need to produce the reference to the outer-class object, you name the outer class followed by a dot and this - sometimes you want to tell some other object to create an object of one of its inner classes. To do this you must provide a reference to the other outer-class object in the new expression, using the .new

How do you initialize fields within an anonymous class? (See TIJ pages 358 to 360.)

- if you're defining an anonymous inner class and want to use an object that's defined outside the anonymous inner class, the compiler requires that the argument reference be final

What is a local inner class and what are its features? (See TIJ 385.)

- inner class created code blacks usually body of method - no access specifier, does have final variables in current code block and all memebers

What is instance initialization? (See TIJ page 361.)

- inside the instance initializer you can see code that couldn't be executed as part of a field initializer - so in effect, an instance initializer is the constructor for an anonymous inner class

What is a Factory Method design pattern and how does it relate to interface? (See TIJ page 339).

- interface is intended to be a gateway to multiple implementations, and a typical way to produce objects that fit the interface is the Factory Method design pattern - instead of calling a constructor directly, you call a creation method on a factory object which produces an implementation of the interface

How are inner classes identified? (See TIJ page 387.)

- name of enclosing class, followed by $, followed by name of inner class - if anonymous, inner class name is numbers - ii nested, $ appended

What is private access? (See TIJ pages 224 to 225.)

- no one can access member except class that contains that member

What is a covariant return type? (See TIJ pages 303 to 304.)

- overridden method in derived class can return type derived from type returned by base class method

What is the meaning of a package statement? (See TIJ page 212.)

- package statement must appear as first non-comment in file - want to see all classes, each own separate .java and .class files, together

What is a control framework? (See TIJ page 375.)

- particular type of application framework dominated by need to respond to events

Where can handles be initialized? (See TIJ pages 239 to 241.)

- point objects defined - constructor for class - before actually need to use object - using instance initialization

What is the appropriate guideline for choosing between classes and interfaces? (See TIJ page 343).

- prefer class to interfaces - start with class, if need to use interface

Why would a programmer write a clean up method? (See TIJ page 251.)

- programmer would write clean up method because don't know when garbage collector called or if it's called

What is the ordering of events in initialization and class loading? (See pages 272 to 274 of TIJ.)

- programs are loaded all at once, as part of the startup process, followed by initialization, and then the program begins - all class in separate files, that file isn't loaded until the code is needed - "class code is loaded at the point of first use." - usually when the first object of that class is constructed, but loading also occurs when a static field or static method is accessed


Kaugnay na mga set ng pag-aaral

Chapter 23: Disorder of red blood cells

View Set

Legal and Ethical Aspects of HIM Ch.'s 1 & 2

View Set

ACCOUNTING ENTRANCE EXAM - Accounting Basics

View Set

Energy Balance and Obesity Chapter 11 Nutrition

View Set

FBLA Entrepreneurship Study Guide

View Set

Chapter 7 Statistics: Sampling Distributions

View Set

Chapters 1-6 Pathology and Anatomy

View Set