Aug 2, 2012
Operators
Relational Operators (Objective 7.6)
- Relational operators always result in a boolean value (true or false).
- There are six relational operators: >, >=, <, <=, ==, and !=.
- The last two (== and !=) are sometimes referred to as equality operators.
- When comparing characters, Java uses the Unicode value of the character as the numerical value.
- There are two equality operators: == and != .
- Four types of things can be tested: numbers, characters, booleans, and reference variables.
- When comparing reference variables, == returns true only if both references refer to the same object.
- instanceof is for reference variables only, and checks for whether the object is of a particular type.
- The instanceof operator can be used only to test objects (or null) against class types that are in the same class hierarchy.
- For interfaces, an object passes the instanceof test if any of its superclasses implement the interface on the right side of the instanceof operator.
- There are four primary math operators: add, subtract, multiply, and divide.
- The remainder operator (%), returns the remainder of a division.
- Expressions are evaluated from left to right, unless you add parentheses, or unless some operators in the expression have higher precedence than others.
- The *, /, and % operators have higher precedence than + and -.
- If either operand is a String, the + operator concatenates the operands.
- If both operands are numeric, the + operator adds the operands.
- Prefix operators (++ and --) run before the value is used in the expression.
- Postfix operators (++ and --) run after the value is used in the expression.
- In any expression, both operands are fully evaluated before the operator is applied.
- Variables marked final cannot be incremented or decremented.
- Returns one of two values based on whether a boolean expression is true or false.
- Returns the value after the ? if the expression is true.
- Returns the value after the : if the expression is false.
- The exam covers six "logical" operators: &, |, ^, !, &&, and ||.
- Logical operators work with two expressions (except for !) that must resolve to boolean values.
- The && and & operators return true only if both operands are true.
- The || and | operators return true if either or both operands are true.
- The && and || operators are known as short-circuit operators.
- The && operator does not evaluate the right operand if the left operand is false.
- The || does not evaluate the right operand if the left operand is true.
- The & and | operators always evaluate both operands.
- The ^ operator (called the "logical XOR"), returns true if exactly one operand is true.
- The ! operator (called the "inversion" operator), returns the opposite value of the boolean operand it precedes.
regards,
Tech Dexters Support Team,
Hyderabad.
for Feedback and Support simply drop a mail in
techdexters{at}gmail.com
Remember to replace the {at} with @ in the email addresses
Jul 24, 2012
Basics
Bullet Points on some Basic Concepts:
Stack and Heap
- Local variables (method variables) live on the stack.
- Objects and their instance variables live on the heap.
Literals and Primitive Casting (Objective 1.3)
- Integer literals can be decimal, octal (e.g. 013), or hexadecimal (e.g. 0x3d).
- Literals for longs end in L or l.
- Float literals end in F or f, double literals end in a digit or D or d.
- The boolean literals are true and false.
- Literals for chars are a single character inside single quotes: 'd'.
Scope (Objectives 1.3 and 7.6)
- Scope refers to the lifetime of a variable.
- There are four basic scopes:
- Static variables live basically as long as their class lives.
- Instance variables live as long as their object lives.
- Local variables live as long as their method is on the stack; however, if their method invokes another method, they are temporarily unavailable.
- Block variables (e.g., in a for or an if) live until the block completes.
Basic Assignments (Objectives 1.3 and 7.6)
- Literal integers are implicitly ints.
- Integer expressions always result in an int-sized result, never smaller.
- Floating-point numbers are implicitly doubles (64 bits).
- Narrowing a primitive truncates the high order bits.
- Compound assignments (e.g. +=), perform an automatic cast.
- A reference variable holds the bits that are used to refer to an object.
- Reference variables can refer to subclasses of the declared type but not to superclasses.
- When creating a new object, e.g., Button b = new Button(); three things happen:
- Make a reference variable named b, of type Button
- Create a new Button object
- Assign the Button object to the reference variable b
Using a Variable or Array Element That Is Uninitialized and Unassigned (Objectives 1.3 and 7.6)
- When an array of objects is instantiated, objects within the array are not instantiated automatically, but all the references get the default value of null.
- When an array of primitives is instantiated, elements get default values.
- Instance variables are always initialized with a default value.
- Local/automatic/method variables are never given a default value. If you attempt to use one before initializing it, you'll get a compiler error.
Passing Variables into Methods (Objective 7.3)
- Methods can take primitives and/or object references as arguments.
- Method arguments are always copies.
- Method arguments are never actual objects (they can be references to objects).
- A primitive argument is an unattached copy of the original primitive.
- A reference argument is another copy of a reference to the original object.
- Shadowing occurs when two variables with different scopes share the same name. This leads to hard-to-find bugs, and hard-to-answer exam questions.
Array Declaration, Construction, and Initialization (Obj. 1.3)
- Arrays can hold primitives or objects, but the array itself is always an object.
- When you declare an array, the brackets can be left or right of the name.
- It is never legal to include the size of an array in the declaration.
- You must include the size of an array when you construct it (using new) unless you are creating an anonymous array.
- Elements in an array of objects are not automatically created, although primitive array elements are given default values.
- You'll get a NullPointerException if you try to use an array element in an object array, if that element does not refer to a real object.
- Arrays are indexed beginning with zero.
- An ArrayIndexOutOfBoundsException occurs if you use a bad index value.
- Arrays have a length variable whose value is the number of array elements.
- The last index you can access is always one less than the length of the array.
- Multidimensional arrays are just arrays of arrays.
- The dimensions in a multidimensional array can have different lengths.
- An array of primitives can accept any value that can be promoted implicitly to the array's declared type;. e.g., a byte variable can go in an int array.
- An array of objects can hold any object that passes the IS-A (or instanceof) test for the declared type of the array. For example, if Horse extends Animal, then a Horse object can go into an Animal array.
- If you assign an array to a previously declared array reference, the array you're assigning must be the same dimension as the reference you're assigning it to.
- You can assign an array of one type to a previously declared array reference of one of its supertypes. For example, a Honda array can be assigned to an array declared as type Car (assuming Honda extends Car).
Initialization Blocks (Objectives 1.3 and 7.6)
- Static initialization blocks run once, when the class is first loaded.
- Instance initialization blocks run every time a new instance is created. They run after all super-constructors and before the constructor's code has run.
- If multiple init blocks exist in a class, they follow the rules stated above, AND they run in the order in which they appear in the source file.
Using Wrappers (Objective 3.1)
- The wrapper classes correlate to the primitive types.
- Wrappers have two main functions:
- To wrap primitives so that they can be handled like objects
- To provide utility methods for primitives (usually conversions)
- The three most important method families are
- xxxValue() Takes no arguments, returns a primitive
- parseXxx() Takes a String, returns a primitive, throws NFE
- valueOf() Takes a String, returns a wrapped object, throws NFE
- Wrapper constructors can take a String or a primitive, except for Character, which can only take a char.
- Radix refers to bases (typically) other than 10; octal is radix = 8, hex = 16.
Boxing (Objective 3.1)
- As of Java 5, boxing allows you to convert primitives to wrappers or to convert wrappers to primitives automatically.
- Using == with wrappers created through boxing is tricky; those with the same small values (typically lower than 127), will be ==, larger values will not be ==.
Advanced Overloading (Objectives 1.5 and 5.4)
- Primitive widening uses the "smallest" method argument possible.
- Used individually, boxing and var-args are compatible with overloading.
- You CANNOT widen from one wrapper type to another. (IS-A fails.)
- You CANNOT widen and then box. (An int can't become a Long.)
- You can box and then widen. (An int can become an Object, via an Integer.)
- You can combine var-args with either widening or boxing.
Garbage Collection (Objective 7.4)
- In Java, garbage collection (GC) provides automated memory management.
- The purpose of GC is to delete objects that can't be reached.
- Only the JVM decides when to run the GC, you can only suggest it.
- You can't know the GC algorithm for sure.
- Objects must be considered eligible before they can be garbage collected.
- An object is eligible when no live thread can reach it.
- To reach an object, you must have a live, reachable reference to that object.
- Java applications can run out of memory.
- Islands of objects can be GCed, even though they refer to each other.
- Request garbage collection with System.gc();
- Class Object has a finalize() method.
- The finalize() method is guaranteed to run once and only once before the garbage collector deletes an object.
- The garbage collector makes no guarantees, finalize() may never run.
- You can uneligibilize an object for GC from within finalize().
regards,
Tech Dexters Support Team,
Hyderabad.
for Feedback and Support simply drop a mail in
techdexters{at}gmail.com
Remember to replace the {at} with @ in the email addresses
Jul 18, 2012
OCPJ Exercise-2 (Object Orientation)
1. Given:
public abstract interface Frobnicate { public void twiddle(String s); }Which is a correct class? (Choose all that apply.) A.
public abstract class Frob implements Frobnicate { public abstract void twiddle(String s) { } }B.
public abstract class Frob implements Frobnicate { }C.
public class Frob extends Frobnicate { public void twiddle(Integer i) { } }D.
public class Frob implements Frobnicate { public void twiddle(Integer i) { } }E.
public class Frob implements Frobnicate { public void twiddle(String i) { } public void twiddle(Integer s) { } }
Answer:
B is correct, an abstract class need not implement any or all of an interface's methods.
E is correct, the class implements the interface method and additionally overloads the twiddle() method.
A is incorrect because abstract methods have no body. C is incorrect because classes implement interfaces they don't extend them. D is incorrect because overloading a method is not implementing it.
(Objective 5.4)
E is correct, the class implements the interface method and additionally overloads the twiddle() method.
A is incorrect because abstract methods have no body. C is incorrect because classes implement interfaces they don't extend them. D is incorrect because overloading a method is not implementing it.
(Objective 5.4)
2. Given:
class Top { public Top(String s) { System.out.print("B"); } } public class Bottom2 extends Top { public Bottom2(String s) { System.out.print("D"); } public static void main(String [] args) { new Bottom2("C"); System.out.println(" "); } }What is the result?
A. BD
B. DB
C. BDC
D. DBC
E. Compilation fails
Answer:
E is correct. The implied super() call in Bottom2's constructor cannot be satisfied because there isn't a no-arg constructor in Top. A default, no-arg constructor is generated by the
compiler only if the class has no constructor defined explicitly.
A, B, C, and D are incorrect based on the above. (Objective 1.6)
A, B, C, and D are incorrect based on the above. (Objective 1.6)
3. Given:
class Clidder { private final void flipper() { System.out.println("Clidder"); } } public class Clidlet extends Clidder { public final void flipper() { System.out.println("Clidlet"); } public static void main(String [] args) { new Clidlet().flipper(); } }What is the result? A. Clidlet
B. Clidder
C. Clidder Clidlet
D. Clidlet Clidder
E. Compilation fails
Answer:
A is correct. Although a final method cannot be overridden, in this case, the method is private, and therefore hidden. The effect is that a new, accessible, method flipper is created. Therefore, no polymorphism occurs in this example, the method invoked is simply that of the child class, and no error occurs.
B, C, D, and E are incorrect based on the preceding.(Objective 5.3)
B, C, D, and E are incorrect based on the preceding.(Objective 5.3)
4. Using the fragments below, complete the following code so it compiles. Note, you may not have to fill all of the slots.
Code:
class AgedP { __________ __________ __________ __________ __________ public AgedP(int x) { __________ __________ __________ __________ __________ } } public class Kinder extends AgedP { __________ __________ __________ _________ ________ __________ public Kinder(int x) { __________ __________ __________ __________ __________ (); } }Fragments: Use the following fragments zero or more times:
AgedP super this ( ) { } ;
Answer:
class AgedP { AgedP() {} public AgedP(int x) { } } public class Kinder extends AgedP { public Kinder(int x) { super(); } }As there is no droppable tile for the variable x and the parentheses (in the Kinder constructor), are already in place and empty, there is no way to construct a call to the superclass constructor that takes an argument. Therefore, the only remaining possibility is to create a call to the noargument superclass constructor. This is done as: super();. The line cannot be left blank, as the parentheses are already in place. Further, since the superclass constructor called is the noargument version, this constructor must be created. It will not be created by the compiler because there is another constructor already present.(Objective 5.4)
5 Which statement(s) are true? (Choose all that apply.) A. Cohesion is the OO principle most closely associated with hiding implementation details
B. Cohesion is the OO principle most closely associated with making sure that classes know about other classes only through their APIs
C. Cohesion is the OO principle most closely associated with making sure that a class is designed with a single, well-focused purpose
D. Cohesion is the OO principle most closely associated with allowing a single object to be seen as having many types
Answer:
Answer C is correct.
A refers to encapsulation, B refers to coupling, and D refers to polymorphism.(Objective 5.1)
A refers to encapsulation, B refers to coupling, and D refers to polymorphism.(Objective 5.1)
6. Given the following,
class X { void do1() { } } class Y extends X { void do2() { } } class Chrome { public static void main(String [] args) { X x1 = new X(); X x2 = new Y(); Y y1 = new Y(); // insert code here } }Which, inserted at line 9, will compile? (Choose all that apply.)
A. x2.do2();
B. (Y)x2.do2();
C. ((Y)x2).do2();
D. None of the above statements will compile
Answer:
C is correct. Before you can invoke Y's do2 method you have to cast x2 to be of type Y. Statement B looks like a proper cast but without the second set of parentheses, the compiler thinks it's an incomplete statement.
A, B and D are incorrect based on the preceding.(Objective 5.2)
A, B and D are incorrect based on the preceding.(Objective 5.2)
7. Given:
1. ClassA has a ClassD
2. Methods in ClassA use public methods in ClassB
3. Methods in ClassC use public methods in ClassA
4. Methods in ClassA use public variables in ClassB
Which is most likely true? (Choose the most likely.) A. ClassD has low cohesion
B. ClassA has weak encapsulation
C. ClassB has weak encapsulation
D. ClassB has strong encapsulation
E. ClassC is tightly coupled to ClassA
Answer:
C is correct. Generally speaking, public variables are a sign of weak encapsulation.
A, B, D, and E are incorrect, because based on the information given, none of these statements can be supported. (Objective 5.1)
A, B, D, and E are incorrect, because based on the information given, none of these statements can be supported. (Objective 5.1)
8. Given:
class Dog { public void bark() { System.out.print("woof "); } } class Hound extends Dog { public void sniff() { System.out.print("sniff "); } public void bark() { System.out.print("howl "); } } public class DogShow { public static void main(String[] args) { new DogShow().go(); } void go() { new Hound().bark(); ((Dog) new Hound()).bark(); ((Dog) new Hound()).sniff(); } }What is the result? (Choose all that apply.)
A. howl howl sniff
B. howl woof sniff
C. howl howl followed by an exception
D. howl woof followed by an exception
E. Compilation fails with an error at line 12
F. Compilation fails with an error at line 13
Answer:
F is correct. Class Dog doesn't have a sniff method.
A, B, C, D, and E are incorrect based on the above information. (Objective 5.2)
A, B, C, D, and E are incorrect based on the above information. (Objective 5.2)
9. Given:
public class Redwood extends Tree { public static void main(String[] args) { new Redwood().go(); } void go() { go2(new Tree(), new Redwood()); go2((Redwood) new Tree(), new Redwood()); } void go2(Tree t1, Redwood r1) { Redwood r2 = (Redwood)t1; Tree t2 = (Tree)r1; } } class Tree { }What is the result? (Choose all that apply.)
A. An exception is thrown at runtime
B. The code compiles and runs with no output
C. Compilation fails with an error at line 6
D. Compilation fails with an error at line 7
E. Compilation fails with an error at line 10
F. Compilation fails with an error at line 11
Answer:
A is correct, a ClassCastException will be thrown when the code attempts to downcast a Tree to a Redwood.
B, C, D, E, and F are incorrect based on the above information.(Objective 5.2)
B, C, D, E, and F are incorrect based on the above information.(Objective 5.2)
10. Given:
public class Tenor extends Singer { public static String sing() { return "fa"; } public static void main(String[] args) { Tenor t = new Tenor(); Singer s = new Tenor(); System.out.println(t.sing() + " " + s.sing()); } } class Singer { public static String sing() { return "la"; } }What is the result?
A. fa fa
B. fa la
C. la la
D. Compilation fails
E. An exception is thrown at runtime
Answer:
B is correct. The code is correct, but polymorphism doesn't apply to static methods.
A, C, D, and E are incorrect based on the above information.(Objective 5.2)
A, C, D, and E are incorrect based on the above information.(Objective 5.2)
11. Given:
class Alpha { static String s = " "; protected Alpha() { s += "alpha "; } } class SubAlpha extends Alpha { private SubAlpha() { s += "sub "; } } public class SubSubAlpha extends Alpha { private SubSubAlpha() { s += "subsub "; } public static void main(String[] args) { new SubSubAlpha(); System.out.println(s); } }What is the result? A. subsub
B. sub subsub
C. alpha subsub
D. alpha sub subsub
E. Compilation fails
F. An exception is thrown at runtime
Answer:
C is correct. Watch out, SubSubAlpha extends Alpha! Since the code doesn't attempt to make a SubAlpha, the private constructor in SubAlpha is okay.
A, B, D, E, and F are incorrect based on the above information. (Objective 5.3)
A, B, D, E, and F are incorrect based on the above information. (Objective 5.3)
12. Given:
class Building { Building() { System.out.print("b "); } Building(String name) { this(); System.out.print("bn " + name); } } public class House extends Building { House() { System.out.print("h "); } House(String name) { this(); System.out.print("hn " + name); } public static void main(String[] args) { new House("x "); } }what is the result?
A. h hn x
B. hn x h
C. b h hn x
D. b hn x h
E. bn x h hn x
F. b bn x h hn x
G. bn x b h hn x
H. Compilation fails
Answer:
C is correct. Remember that constructors call their superclass constructors, which execute first, and that constructors can be overloaded.
A, B, D, E, F, G, and H are incorrect based on the above information.(Objectives 1.6, 5.4)
A, B, D, E, F, G, and H are incorrect based on the above information.(Objectives 1.6, 5.4)
13. Given:
class Mammal { String name = "furry "; String makeNoise() { return "generic noise"; } } class Zebra extends Mammal { String name = "stripes "; String makeNoise() { return "bray"; } } public class ZooKeeper { public static void main(String[] args) { new ZooKeeper().go(); } void go() { Mammal m = new Zebra(); System.out.println(m.name + m.makeNoise()); } }What is the result?
A. furry bray
B. stripes bray
C. furry generic noise
D. stripes generic noise
E. Compilation fails
F. An exception is thrown at runtime
Answer:
A is correct. Polymorphism is only for instance methods.
B, C, D, E, and F are incorrect based on the above information.(Objectives 1.5, 5.4)
B, C, D, E, and F are incorrect based on the above information.(Objectives 1.5, 5.4)
14. You're designing a new online board game in which Floozels are a type of Jammers, Jammers can have Quizels, Quizels are a type of Klakker, and Floozels can have several Floozets. Which of the following fragments represent this design? (Choose all that apply.)
A.
import java.util.*; interface Klakker { } class Jammer { SetB.q; } class Quizel implements Klakker { } public class Floozel extends Jammer { List f; } interface Floozet { }
import java.util.*; class Klakker { SetC.q; } class Quizel extends Klakker { } class Jammer { List f; } class Floozet extends Floozel { } public class Floozel { Set k; }
import java.util.*; class Floozet { } class Quizel implements Klakker { } class Jammer { ListD.q; } interface Klakker { } class Floozel extends Jammer { List f; }
import java.util.*; interface Jammer extends Quizel { } interface Klakker { } interface Quizel extends Klakker { } interface Floozel extends Jammer, Floozet { } interface Floozet { }
Answer:
A and C are correct. The phrase "type of" indicates an "is-a" relationship (extends or implements), and the phrase “have” is of course a "has-a" relationship (usually instance variables).
B and D are incorrect based on the above information. (Objective 5.5)
B and D are incorrect based on the above information. (Objective 5.5)
15. Given:
class A { } class B extends A { } public class ComingThru { static String s = "-"; public static void main(String[] args) { A[] aa = new A[2]; B[] ba = new B[2]; sifter(aa); sifter(ba); sifter(7); System.out.println(s); } static void sifter(A[]... a2) { s += "1"; } static void sifter(B[]... b1) { s += "2"; } static void sifter(B[] b1) { s += "3"; } static void sifter(Object o) { s += "4"; } }What is the result?
A. -124
B. -134
C. -424
D. -434
E. -444
F. Compilation fails
Answer:
D is correct. In general, overloaded var-args methods are chosen last. Remember that arrays are objects. Finally, an int can be boxed to an Integer and then "widened" to an Object.
A, B, C, E, and F are incorrect based on the above information.(Objective 1.5)
A, B, C, E, and F are incorrect based on the above information.(Objective 1.5)
regards,
Tech Dexters Support Team,
Hyderabad.
for Feedback and Support simply drop a mail in
techdexters{at}gmail.com
Remember to replace the {at} with @ in the email addresses
Jul 11, 2012
Object Orientation
Encapsulation, IS-A, HAS-A (Objective 5.1)
- Encapsulation helps hide implementation behind an interface (or API).
- Encapsulated code has two features:
- Instance variables are kept protected (usually with the private modifier).
- Getter and setter methods provide access to instance variables.
- IS-A refers to inheritance or implementation.
- IS-A is expressed with the keyword extends.
- IS-A, "inherits from," and "is a subtype of " are all equivalent expressions.
- 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)
- 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, polymorphism, overriding, overloading, and casting.
- All classes (except class Object), are subclasses of type Object, and therefore they inherit Object's methods.
Polymorphism (Objective 5.2)
- Polymorphism means "many forms."
- A reference variable is always of a single, unchangeable type, but it can refer to a subtype 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.
- The reference variable's type (not the object's type), determines which methods can be called!
- Polymorphic method invocations apply only to overridden instance methods.
Overriding and Overloading (Objectives 1.5 and 5.4)
- Methods can be overridden or overloaded; constructors can be overloaded but not overridden.
- Abstract methods must be overridden by the first concrete (non-abstract) subclass.
- 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.
- final methods cannot be overridden.
- Only inherited methods may be overridden, and remember that private methods are not inherited.
- A subclass uses super.overriddenMethodName() to call the superclass version of an overridden method.
- Overloading means reusing a method name, but with different arguments.
- 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
- Methods from a superclass can be overloaded in a subclass.
- Polymorphism applies to overriding, not to overloading.
- Object type (not the reference variable's type), determines which overridden method is used at runtime.
- Reference type determines which overloaded method will be used at compile time.
Reference Variable Casting (Objective 5.2)
- There are two types of reference variable casting: downcasting and upcasting.
- 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 anexplicit cast to do this, and the result is that you can access the subtype's members with this new reference variable.
- 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)
- When you implement an interface, you are fulfilling its contract.
- You implement an interface by properly and concretely overriding all of the methods defined by the interface.
- A single class can implement many interfaces.
Return Types (Objective 1.5)
- Overloaded methods can change return types; overridden methods cannot, except in the case of covariant returns.
- Object reference return types can accept null as a return value.
- An array is a legal return type, both to declare and return as a value.
- For methods with primitive return types, any value that can be implicitly converted to the return type can be returned.
- 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.
- Methods with an object reference return type, can return a subtype.
- Methods with an interface return type, can return any implementer.
Constructors and Instantiation (Objectives 1.6 and 5.4)
- A constructor is always invoked when a new object is created.
- Each superclass in an object's inheritance tree will have a constructor called.
- Every class, even an abstract class, has at least one constructor.
- Constructors must have the same name as the class.
- 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.
- 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 of the actual instance being created.
- Constructors can use any access modifier (even private!).
- The compiler will create a default constructor if you don't create any constructors in your class.
- The default constructor is a no-arg constructor with a no-arg call to super().
- The first statement of every constructor must be a call to either this() (an overloaded constructor) or super().
- The compiler will add a call to super() unless you have already put in a call to this() or super().
- Instance members are accessible only after the super constructor runs.
- Abstract classes have constructors that are called when a concrete subclass is instantiated.
- Interfaces do not have constructors.
- 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.
- Constructors are never inherited, thus they cannot be overridden.
- A constructor can be directly invoked only by another constructor (using a call to super() or this()).
- Issues with calls to this()
- May appear only as the first statement in a constructor.
- The argument list determines which overloaded constructor is called.
- 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.
- 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)
- Use static methods to implement behaviors that are not affected by the state of any instances.
- 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.
- All static members belong to the class, not to any instance.
- A static method can't access an instance variable directly.
- static methods can't be overridden, but they can be redefined.
- 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();
Coupling and Cohesion (Objective 5.1)
- Coupling refers to the degree to which one class knows about or uses members of another class.
- 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.
- Tight coupling is the undesirable state of having classes that break the rules of loose coupling.
- Cohesion refers to the degree in which a class has a single, well-defined role or responsibility.
- High cohesion is the desirable state of a class whose members support a single, well-focused role or responsibility.
- Low cohesion is the undesirable state of a class whose members support multiple, unfocused roles or responsibilities.
regards,
Tech Dexters Support Team,
Hyderabad.
for Feedback and Support simply drop a mail in
techdexters{at}gmail.com
Remember to replace the {at} with @ in the email addresses
Subscribe to:
Posts (Atom)