Java Theory Full

Identifiers 

  • Identifiers can begin with a letter, an underscore, or a currency character.
  • After the first character, identifiers can also include digits.
  • Identifiers can be of any length.
  • JavaBeans methods must be named using camelCase, and depending on the
  • method's purpose, must start with set, get, is, add, or remove.


Declaration Rules  

  • A source code file can have only one public class.
  • If the source file contains a public class, the filename must match the
  • public class name.
  • A file can have only one package statement, but multiple imports.
  • The package statement (if any) must be the first (non-comment) line in a
  • source file.
  • The import statements (if any) must come after the package and before
  • the class declaration.
  • If there is no package statement, import statements must be the first (noncomment)
  • statements in the source file.
  • package and import statements apply to all classes in the file.
  • A file can have more than one nonpublic class.
  • Files with no public classes have no naming restrictions.


Class Access Modifiers 

  • There are three access modifiers: public, protected, and private.
  • There are four access levels: public, protected, default, and private.
  • Classes can have only public or default access.
  • A class with default access can be seen only by classes within the same package.
  • A class with public access can be seen by all classes from all packages.
  • Class visibility revolves around whether code in one class can
  • Create an instance of another class
  • Extend (or subclass), another class
  • Access methods and variables of another class


Class Modifiers (Nonaccess)

  • Classes can also be modified with final, abstract, or strictfp.
  • A class cannot be both final and abstract.
  • A final class cannot be subclassed.
  • An abstract class cannot be instantiated.
  • A single abstract method in a class means the whole class must be abstract.
  • An abstract class can have both abstract and nonabstract methods.
  • The first concrete class to extend an abstract class must implement all of its
  • abstract methods.


Interface Implementation 

  • Interfaces are contracts for what a class can do, but they say nothing about the way in which the class must do it.
  • Interfaces can be implemented by any class, from any inheritance tree.
  • An interface is like a 100-percent abstract class, and is implicitly abstract whether you type the abstract modifier in the declaration or not.
  • An interface can have only abstract methods, no concrete methods allowed.
  • Interface methods are by default public and abstract—explicit declaration of these modifiers is optional.
  • Interfaces can have constants, which are always implicitly public,  static, and final.
  • Interface constant declarations of public, static, and final are optional in any combination.
  • A legal nonabstract implementing class has the following properties:
  • It provides concrete implementations for the interface's methods.
  • It must follow all legal override rules for the methods it implements.
  • It must not declare any new checked exceptions for an implementation method.


Declarations and Access Control

  • It must not declare any checked exceptions that are broader than the exceptions declared in the interface method.
  • It may declare runtime exceptions on any interface method implementation regardless of the interface declaration.
  • It must maintain the exact signature (allowing for covariant returns) and return type of the methods it implements (but does not have to declare the exceptions of the interface).
  • A class implementing an interface can itself be abstract.
  • An abstract implementing class does not have to implement the interface methods (but the first concrete subclass must).
  • A class can extend only one class (no multiple inheritance), but it canimplement many interfaces.
  • Interfaces can extend one or more other interfaces.
  • Interfaces cannot extend a class, or implement a class or interface.
  • When taking the exam, verify that interface and class declarations are legal before verifying other code logic.


Member Access Modifiers  

  • Methods and instance (nonlocal) variables are known as "members."
  • Members can use all four access levels: public, protected, default, private.
  • Member access comes in two forms:
  • Code in one class can access a member of another class.
  • A subclass can inherit a member of its superclass.
  • If a class cannot be accessed, its members cannot be accessed.
  • Determine class visibility before determining member visibility.
  • public members can be accessed by all other classes, even in other packages.
  • If a superclass member is public, the subclass inherits it—regardless of package.
  • Members accessed without the dot operator (.) must belong to the same class.
  • this. always refers to the currently executing object.
  • this.aMethod() is the same as just invoking aMethod().
  • private members can be accessed only by code in the same class.
  • private members are not visible to subclasses, so private members cannot be inherited.
  • Default and protected members differ only when subclasses are involved:
  • Default members can be accessed only by classes in the same package.
  • protected members can be accessed by other classes in the same
  • package, plus subclasses regardless of package.
  • protected = package plus kids (kids meaning subclasses).
  • For subclasses outside the package, the protected member can beaccessed only through inheritance; a subclass outside the package cannot  access a protected member by using a reference to a superclass instance (in other words, inheritance is the only mechanism for a subclass outside the package to access a protected member of its superclass).
  • A protected member inherited by a subclass from another package is not accessible to any other class in the subclass package, except for the subclass' own subclasses.


Local Variables 

  • Local (method, automatic, or stack) variable declarations cannot have access modifiers.
  • final is the only modifier available to local variables.
  • Local variables don't get default values, so they must be initialized before use. Other Modifiers—Members (Objective 1.3)
  • final methods cannot be overridden in a subclass.
  • abstract methods are declared, with a signature, a return type, and an optional throws clause, but are not implemented.
  • abstract methods end in a semicolon—no curly braces.
  • Three ways to spot a non-abstract method:
  • The method is not marked abstract.
  • The method has curly braces.
  • The method has code between the curly braces.
  • The first nonabstract (concrete) class to extend an abstract class must implement all of the abstract class' abstract methods.
  • The synchronized modifier applies only to methods and code blocks.
  • synchronized methods can have any access control and can also be marked final.


Declarations and Access Control

  • abstract methods must be implemented by a subclass, so they must be inheritable. For that reason:
  • abstract methods cannot be private.
  • abstract methods cannot be final.
  • The native modifier applies only to methods.
  • The strictfp modifier applies only to classes and methods.
  • Methods with var-args 
  • As of Java 5, methods can declare a parameter that accepts from zero to many arguments, a so-called var-arg method.
  • A var-arg parameter is declared with the syntax type... name; for instance: doStuff(int... x) { }
  • A var-arg method can have only one var-arg parameter.
  • In methods with normal parameters and a var-arg, the var-arg must come last.


Variable Declarations  

  • Instance variables can
  • Have any access control
  • Be marked final or transient
  • Instance variables can't be abstract, synchronized, native, or strictfp.
  • It is legal to declare a local variable with the same name as an instance variable; this is called "shadowing."
  • final variables have the following properties:
  • final variables cannot be reinitialized once assigned a value.
  • final reference variables cannot refer to a different object once the object has been assigned to the final variable.
  • final reference variables must be initialized before the constructor completes.
  • There is no such thing as a final object. An object reference marked final does not mean the object itself is immutable.
  • The transient modifier applies only to instance variables.
  • The volatile modifier applies only to instance variables.


Array Declarations 

  • Arrays can hold primitives or objects, but the array itself is always an object.
  • When you declare an array, the brackets can be to the left or right of thevariable name.
  • It is never legal to include the size of an array in the declaration.
  • 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. Static Variables and Methods (Objective 1.4)
  • They are not tied to any particular instance of a class.
  • No classes instances are needed in order to use static members of the class.
  • There is only one copy of a static variable / class and all instances share it.
  • static methods do not have direct access to non-static members.


Enums

  • An enum specifies a list of constant values assigned to a type.
  • An enum is NOT a String or an int; an enum constant's type is the enum type. For example, SUMMER and FALL are of the enum type Season.
  • An enum can be declared outside or inside a class, but NOT in a method.
  • An enum declared outside a class must NOT be marked static, final, abstract, protected, or private.
  • Enums can contain constructors, methods, variables, and constant class bodies.
  • enum constants can send arguments to the enum constructor, using the syntax BIG(8), where the int literal 8 is passed to the enum constructor.
  • enum constructors can have arguments, and can be overloaded.
  • enum constructors can NEVER be invoked directly in code. They are always called automatically when an enum is initialized.
  • The semicolon at the end of an enum declaration is optional. These are legal:enum Foo { ONE, TWO, THREE} enum Foo { ONE, TWO, THREE};
  • MyEnum.values() returns an array of MyEnum's values.

Encapsulation, IS-A, HAS-A  

  • 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 

  • 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

  • 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 
  • 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 

  • 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 an explicit 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 
  • 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 
  • 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
  • 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 
  • 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.
  • 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();
  • static methods can't be overridden, but they can be redefined.

Coupling and Cohesion 
  • 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.

SOURCE : SCJP Sun Certified Programmer for Java 6-835.pdf

No comments:

Post a Comment