Two-Minute Drill


Here are some of the key points from each certification objective in this chapter.

Encapsulation, IS-A, HAS-A (Objective 5.1)

q  

Encapsulation helps hide implementation behind an interface (or API).

q  

Encapsulated code has two features:

q  

Instance variables are kept protected (usually with the private modifier).

q  

Getter and setter methods provide access to instance variables.

q  

IS-A refers to inheritance.

q  

IS-A is expressed with the keyword extends.

q  

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

q  

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

Inheritance (Objective 5.5)

q  

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

q  

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

q  

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

Polymorphism (Objective 5.2)

q  

Polymorphism means "many forms."

q  

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

q  

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.

q  

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

q  

Polymorphic method invocations apply only to overridden instance methods.

Overriding and Overloading (Objectives 1.5 and 5.4)

q  

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

q  

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

q  

With respect to the method it overrides, the overriding method

q  

Must have the same argument list.

q  

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.

q  

Must not have a more restrictive access modifier.

q  

May have a less restrictive access modifier.

q  

Must not throw new or broader checked exceptions.

q  

May throw fewer or narrower checked exceptions, or any unchecked exception.

q  

final methods cannot be overridden.

q  

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

q  

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

q  

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

q  

Overloaded methods

q  

Must have different argument lists

q  

May have different return types, if argument lists are also different

q  

May have different access modifiers

q  

May throw different exceptions

q  

Methods from a superclass can be overloaded in a subclass.

q  

Polymorphism applies to overriding, not to overloading.

q  

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

q  

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

Reference Variable Casting (Objective 5.2)

q  

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

q  

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.

q  

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

Implementing an Interface (Objective 1.2)

q  

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

q  

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

q  

A single class can implement many interfaces.

Return Types (Objective 1.5)

q  

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

q  

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

q  

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

q  

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

q  

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

q  

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

q  

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

Constructors and Instantiation (Objectives 1.6 and 5.4)

q  

A constructor is always invoked when a new object is created.

q  

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

q  

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

q  

Constructors must have the same name as the class.

q  

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.

q  

Typical constructor execution occurs as follows:

q  

The constructor calls its superclass constructor, which calls its superclass constructor, and so on all the way up to the Object constructor.

q  

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 of the actual instance being created.

q  

Constructors can use any access modifier (even private!).

q  

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

q  

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

q  

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

q  

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

q  

Instance members are accessible only after the super constructor runs.

q  

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

q  

Interfaces do not have constructors.

q  

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.

q  

Constructors are never inherited, thus they cannot be overridden.

q  

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

q  

Issues with calls to this()

q  

May appear only as the first statement in a constructor.

q  

The argument list determines which overloaded constructor is called.

q  

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.

q  

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

Statics (Objective 1.3)

q  

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

q  

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.

q  

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

q  

A static method can't access an instance variable directly.

q  

Use the dot operator to access static members, but remember that using a reference variable with the dot operator is really a syntax trick, and the compiler will substitute the class name for the reference variable, for instance:

 d.doStuff(); 

becomes:

 Dog.doStuff(); 

q  

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

Coupling and Cohesion (Objective 5.1)

q  

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

q  

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.

q  

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

q  

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

q  

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

q  

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




SCJP Sun Certified Programmer for Java 5 Study Guide Exam 310-055
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press)
ISBN: 0072253606
EAN: 2147483647
Year: 2006
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net