Card Set Information

2011-11-05 09:33:09

Show Answers:

  1. What is a Class?
    A template that describes the kinds of state and behavior that objects of its type support.
  2. What is an Object?
    At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object which is an instance of that class. That object will have its own state, and access to all of the behaviors defined by its class.
  3. What is Inheritance?
    Object oriented concept which allows code defined in one class to be reused in other classes.
  4. What are Interfaces?
    • Importants in inheritance. Interfaces are like a 100% abstract superclass that defines the methos a subclass must support (just the signature), but the subclass has to implement the methods.
    • An Interface is a Contract.
    • All interface methods are implicity public and abstracts. Must not be static, final, strictfp or native.
    • All variables must be public, static, and final. (constants). Implicity.
    • Can extend one or more other interfaces, and just interfaces.
  5. What means Cohesive?
    Means that every class should have a focused set of responsabilities.
  6. Java Identifiers
    • Identifiers are the names for mathos, classes and variables. Legal identifiers:
    • Must start with a letter, currency character ($), or underscore ( _ ) . Cannot start with number.
    • There is no limit to number of characters.
    • Cannot use java keywords as identifiers.
    • Idetifiers are case sensitive.
  7. Java Keywords
  8. Examples of JavaBean method signature
    • public void setMyValue(int v)
    • public int getMyValue()
    • public boolean isMyStatus()
    • public void addMyListener(MyListener m)
    • public void removeMyListener(MyListener m)
  9. which are the sorce file declaration rules?
    • One public class per source code file
    • Comments can appear at the beginning or end of any line in the source code.
    • The name of the file must match the cname of the public class.
    • The package statement must be the first line.
    • The import statements, must go between the package statement and the class declaration.
    • A file can have more than one nonpublic class.
    • Files with no public classes can have a name that does not match any of the classes in the file
  10. Default Class Access
    • No modifier preceding the declaration.
    • Package level access. A class in a different package cannot have access.
  11. Public Class Access
    Class can be accessed from all class in any package.
  12. strictfp Class modifier
    For the exam it is just necessary to know that strictfp is a keyword and can be used to modify a class or a method, but never a variable.
  13. Final class modifier
    • A Final class cannot be subclassed.
    • No other class can ever extend a final class.
    • String class is final.
    • A final class destroy extensibility OO benefit.
  14. Abstract class modifier
    • An abstract class can never be instantiated.
    • Its only mission is to be extended (subclassed).
    • An abstract class finish with semicolon ( ; )
    • public abstract void goFast();
    • If one method is abstract the whole class must be abstract.
  15. Public Members
    • Public methods or variable members can be accessed by all classes regardless of the package they belong.
    • It depends of class access.
  16. Private Members
    • Can't be accessed by code in any class but the class in which the private member was declarated.
    • A subclass can't inherit private members.
    • Rules of overriding do not apply so a method with the same name is totally a diferent method.
  17. Default Members
    A default member can be accessed ony if the class accessng the member belongs to the same package.
  18. Protected Members
    • A protected member can be accessed by classes that belong to the same package.
    • A protected member can be accesses by a subclass even is the subclass is in a different package.(different to default access)
    • Protected members in the subclass become private to any code outside the subclass, with exception of subclasses of the subclass.
  19. Local variables and access modifiers
    The only modifier allowed in local variables is final.
  20. Final Methods
    • Final keyword prevents a method from being overriden in a subclass.
    • Final keyword is contrary to many OO befefits like extensibility.
  21. Final Arguments
    • A final argument can't be modified within the method.
    • public Record getRecord(int fileNumber, final int recordNumber)
  22. Abstracts Methods
    • An abstract method does not contain funcional code.
    • An abstract method finish with a semicolon.
    • It has no method body.
    • Subclasses have to provide the implementacion.
    • A method can never be abstract and final or abstract and private or abstract and static.
    • public abstract void showSample();
    • If a class has an abstract method, the class has to be obligatory declared abstract .
    • It is legal to have an abstract class with no abstract methods.
    • The first concrete (nonabstract) subclass of an abstract class must implement all abstract methods.
  23. Synchronized Methods
    • Synchronized keyword means that a method can be accessed by only one thread at a time.
    • It can be applied only to methods (not classes, not variables)
    • public synchronized Record retrieveUserInfo(int id) { }
  24. Native Methods
    • The native modifier Indicates that a method is implemented in a platform-dependent code, often C.
    • Can be applied only to methods.
    • A native method's body must be a semicolon like in abstract methods.
  25. Strictfp Methods
    Strictfp forces floating points to adhere to the IEEE 754 standard.
  26. Variable Argument List (var-args)
    • Methods that can take a variable number of arguments.
    • The var-arg must be the last argument in the method's signature.
    • A method can have just one var-arg.
    • void doStuff2(char c, int... x)
  27. Constructor Declarations
    • A constructor does not have a return type.
    • Have the same name as the class.
    • Constructors can't be marked as static, final or abstract
  28. Primitive Variables
    • char, boolean, byte, short, int, long, double and floar.
    • Once a primitive has been declared, its primitive type can never change.
    • Can be declared as class variables (static), instance variables, method parameters or local variables.
  29. Reference Variables
    • Used to refer or access an object.
    • Can refer any object of the declared type or a subtype of the declared type.
  30. Numeric Types
    • byte -> short -> int -> long (integers)
    • float -> double (floating point)
    • All number types are signed (+ and -)
    • The leftmost bit is used to represent the sign, 1 means negative and 0 means positive.
  31. Range of Numeric Primitives
  32. Instance Variables
    • Defined inside the class but outside aany method.
    • Initialized when the class is instantiated.
    • Can use any of the four access levels (default, pretected, private and public)
    • Can be marked as final and transient.
    • Cannot be marked as abstract, synchronized, strictfp, native, static.
  33. Comparation of modifiers on variables vs methods
  34. Local Variables
    • Declared within a method.
    • Variables live in the scope of the method.
    • They are destroyed when the method has completed.
    • Local variables are always on the stack, not in the heap.
    • Local variables must be initialized before use it.
    • Local variables do not get default values.
    • It is possible to make shadowing (local variable with the same name than an instance variable)
  35. Array Declaration
    • Arrays can store multiple variables of the same type (or subclasses).
    • Array is an object on the heap.
    • int[] key;
    • Thread threads [];
    • String [] [] [] name; (array of arrays of arrays)
    • String [] lastName []; (array of arrays)
  36. Final Variables
    • A final variable is impossible to reinitialize once it has been initialized.
    • A primitive final variables the value can't ve altered.
    • A reference final variable can't ever be reassigned to refer to a different object. The data can be changed but the reference variable cannot.
  37. Transient Variables
    A transient variable will be ignored by the JVM when attempt to serialize the object containing it.
  38. Volatile Variables
    • Volatile modifier tells the JVM that a thread accessing the variable must always ajust its own private copy of the variable with the master copy in memory.
    • It can be applied only to instance vatiables.
  39. Static Variables and Methods
    • The static members will exist independently of any instance of the class.
    • Static members exist before any new instance of the class.
    • There will be only one copy of the static members.
    • All instances of the class share the same value for any static variable.
  40. Declaring Enums
    • Lets restrict a variable to having one of the only few pre-defined values.
    • Can be declared as their own separate class, or as a class member.
    • Cannot be declared within a method.
    • Enum declared outside a class cannot be private or protected. (can be public or default).
    • enum CoffeeSize{BIG,HUGE}
    • drink.size = CoffeeSize.BIG; // enum outside class
    • drink.size = Coffee2.CoffeeSize.BIG; enum inside class
    • It is optional tu put a semicolon if the enum is the las declaration.
  41. Enum constructor and instance variable declaration
  42. Use Emum Class
  43. Enum Constructor Overriding
  44. Encapsulation
    • One benefit of encapsulation is the ability to make changes to the code without breaking the code of others who use the same code.
    • Hide implementation details behind an interface. That interface is a set of accessible methods (API).
    • Encapsulation benefits are maintainability, flexibility, and extensibility.
    • Rules: keep intance variables protected (private), make public accessor methods, and use the javaBean naming convention (get and set).
  45. Inheritance
    • Inheritance promote code reuse and polymorphism.
    • Every class is a subclass of class Object.
    • All classes inherit the methos: equals, clone, notify, wait and others.
  46. References
    • A reference variable can be of onle one type, although the object it references can chage.
    • A reference is a variable, so it can be reassigned to other objects. (excluding final references).
    • A reference variable determines the methods that can be invoked on the object the rariable is refering.
    • A reference variable can refer to any subtype of the declared type.
    • A reference variable can be declared as a class or an iterface.
    • At runtime the JVM knows the object, so if the object has an overridden method, the JVM will invoke the overridden version, and not the one of the declared reference variable type.
  47. Overridden Methods
    • A class that inherit a method from a superclass have the can override the method.
    • The benefit is to define behavior that is specific to a subclass.
    • Abstracts methos have to be implemented (overridden) by the concrete class.
    • The overriden method has to have the same or better access (public, protected..)
    • The return type must be the same or a subtype of the superclass method.
    • The overriding method can throw any unchecked (runtime) exception, even if the overridden method does not.
    • The overriding method must not throw checked exception that are new or broader.
    • Overriding method can throw narrower or fewer exceptions.
    • It is not possible to override a method neither marked final nor marked static.
    • Then a method declares a cheked exception but the overriding method does not, the compiler thinks you are calling a method that declares an exception, so if the overriding method does not declare the exception and a reference of the superclass refers to the subtype, the compiler throws an error. This would not occurr at runtime.
  48. Overloaded Methods
    • Overloaded methods let reuse the same method name in a class, but with different arguments (optionally different return type).
    • Overloaded methods must change the argument list.
    • Overloaded methods can change the return type.
    • Overloaded methods can change access modifiers.
    • Overloaded methods can declare new or broader checked exceptions.
    • A method can be overloaded in the same class or in a subclass.
    • Overloaded method are chosen on compilation time and not in runtime, so which overloaded version of the method to call is based on the reference type of the argument passed at compile time.
  49. Differences between overloaded an overrriden methods.
  50. Implementin Interface
    • Provide concrete implementation for all methods.
    • Follow all the rules for legal overrides.
    • Decalre no checked exception on implementation methods other than those declared by the interface method or a subclass of those declared by the interface.
    • Maintain the signature of the interface method, and maintain the same return type or subtype.
    • Implementig defines a role the class can play but not who or what the class is.
    • A class can implement more than one interface.
    • An interface can extend another intreface.
  51. Return Types on Overloaded Methods
    The overloaded method can have a different return type, but it is obligatory to chage the argument list.
  52. Overriding and Return Types
    A subclass must define a method that matched the inherited version. However, the return type can also be a subtype of the original declared return type.
  53. Return Value Rules (1)
    • You can return null in a method with an object reference return type.
    • An array is perfectly legal return type.
    • In a method with a primitive return type, you can return any value or variable that can be implicitly converted to the declared return type. public int foo() {char c='c'; return c;}
    • In a method with a primitive return type, you can return any value or variable that can be explicitly converted to the declared return type. public int foo() {float f=32.5f; return (int)f;}
  54. Return Value Rules (2)
    • In a method with an object reference return type, you can return any type that can be implicitly cast to the declared return type.
  55. Return Value Type (3)
  56. Constructor Basic
    • Every class, including abstract classes, must have a constructor.
    • Constructors do not have return type and the name matchs the class name.
    • Constructors are invoked at runtime with the new keyword.
  57. Constructors Rules
    • Can use any access modifier, including private (a private constructor means that only code within the class can instantiate an object of that type).
    • If you do not type a constructor a default constructor will be automatically generated by the compiler.
    • If the class has a constructor with arguments the compiler will not create one with no arguments.
    • Every constructor has as its first stament a call to an overloaded costructor (this()) or a call to the superclass constructor (super()).
    • You cannot call to an instance method or access an intance vatiable until after the super constructor runs.
    • Only static variables and methods can be accessed as part of the call to super or this. ex: super(Animal.NAME).
    • Abstract classes have constructors.
    • Interfaces does not have constructors.
    • Constructor can be invoked only within other contructor.
    • When a constructor has a this() call to other constructor, sooner or later the super() constructor gets callled.
    • The first line of a constructor must be super() or this(). If the constructor does not have any call the compiler insert the no-arg call to super().
    • A constructor can never have both a call to super() and a call to this() because each of those calls must be the first stament.
  58. Compilet-Generated Constructor Code
  59. Static Variables and Methods
    • Static variables are seted when the JVM load the class, it is before instances are created.
    • Static variables and methods belong to the class, rather than to any particular instance.
    • It is possible to use static members without having an instance of the class.
    • There is only one copy of the static members.
    • A static method cannot access a nonstatic (instance) variable or method. It is necesary to have an instance of the class to access instance members.
  60. Accessing Static Methods and Variables
    • It is not neccessary an instance to access static members.
    • To access a static method or variable you need the name of the class and the dot operator.
    • It is also possible to access the static members using an instance of the class.
    • Static methods cannot be overriden, but they can be redefined in a subclass.
  61. Coupling
    • Coupling is the degree to which one class knows about another class.
    • If a class know about other class through its interface, then both classes are loosely coupled.
    • If a class relies on parts of another class that are not part of the class interface, then the coupling is tighter.
  62. Cohesion
    • Cohesion is used to indicate the degree to which a class has a sigle, well-focused purpose.
    • Cohesion let a much easier maintainace and reusabillity.