| Encapsulation | Encapsulation is a core of Object Oriented (OO) Design.
Allows software reuse. |
|||||||||||||||||||||||||||||||
OO concepts:
|
||||||||||||||||||||||||||||||||
| It has to do with the visibility, coupling, of an implementation to outside classes. | ||||||||||||||||||||||||||||||||
| It determines whether a method and instance variables are public, private or protected and so on. | ||||||||||||||||||||||||||||||||
| A design goals of OO Design is providing minimal coupling through modularity. | ||||||||||||||||||||||||||||||||
| Code are separate into classes. Their internal implementation is hidden. | ||||||||||||||||||||||||||||||||
| Encapsulation provides code clarity. | ||||||||||||||||||||||||||||||||
| Class Definition | [keyword] class Myclass [extends Superclas] [implements Interface]{
class variables static initializer instance variables constructors class methods instance methods }
Class keywords: public, abstract and final |
|||||||||||||||||||||||||||||||
| Visibility | Visibility is the accessibility of methods and instance variables to other classes and packages. | |||||||||||||||||||||||||||||||
| When implementing a class, methods and variables visibility is determined by the keywords: public, private, protected or package (default). | ||||||||||||||||||||||||||||||||
| Modular Code | A class is a module which contains data members. Data and methods. | |||||||||||||||||||||||||||||||
| Modular code keeps implementation isolated so that they depend on other code as little as possible. | ||||||||||||||||||||||||||||||||
| coupling | The class code should reduce coupling. In coupling, data members are dependent on each other internal implementation. This does not allow isolated changes. Revisions, or reuse are harder. Testing and validation is dependent on other classes. | |||||||||||||||||||||||||||||||
| By creating modular code and specialized classes, coupling is reduced. | ||||||||||||||||||||||||||||||||
| MethodInterface (access) | A key concept of encapsulation is providing interface (method access) of the class to the outside classes. | |||||||||||||||||||||||||||||||
| How outside classes use and access internal data. | ||||||||||||||||||||||||||||||||
| Implementation of the class may change, but the method signature should remain the same (name, parameter list and return type). | ||||||||||||||||||||||||||||||||
| To protect the data, data should be made private and provide method access. | ||||||||||||||||||||||||||||||||
| Access Modifiers | public data – Accessible to all classes
private data – Accessible to immediate class only
protected – Access to immediate class, subclass in immediate superclass, or outside package.
none – Access to classes in immediate package |
|||||||||||||||||||||||||||||||
| It’s best to use (accessor) methods -get’s.It’s best to use (mutator) methods -set’s. | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| Another technique to protect the constructing of objects is to use interface. | ||||||||||||||||||||||||||||||||
| Interface | Used when a class must be associated and treated like more than one type of class. | |||||||||||||||||||||||||||||||
| The class is associated with other (different) behaviors and data.
Crosses class hierarchy |
||||||||||||||||||||||||||||||||
The interface has these members:
|
||||||||||||||||||||||||||||||||
| Data members are public and constant | ||||||||||||||||||||||||||||||||
| There is no constructor for an interface | ||||||||||||||||||||||||||||||||
| Protects class constructors:Use a “create” method to create an new object and return that object. This create method can be made static. | ||||||||||||||||||||||||||||||||
| Extensible | Extensibility in encapsulation concerns with the ease of adding new code to existing classes. | |||||||||||||||||||||||||||||||
| extends keyword | Extending the base classes to create new classes makes life easier. | |||||||||||||||||||||||||||||||
| Another benefit of encapsulation is code clarity. | ||||||||||||||||||||||||||||||||
| is a / has a | They define the relationship of classes. Whether a class is a subclass of a class of by composition, contained within another class. | |||||||||||||||||||||||||||||||
| is a | A basic rule of inheritance is that a subclass must be a type of the base class. If it is not a type of the base class, the inheritance rule is violated. | |||||||||||||||||||||||||||||||
| Usually creating new class hierarchy would resolve this issue.Otherwise the new class should be a member of the class. | ||||||||||||||||||||||||||||||||
| has a | A class is a member of another class (composition relation) | |||||||||||||||||||||||||||||||
| Testing class relation | Is a test, answer the question: “Is the derived class a type of the base class.” If so, we have a “is a” relationship.
Has a test, answer the question: “Does the first class contain one or more of the second class.” If so, we have a “has a” relationship. |
|||||||||||||||||||||||||||||||
| instanceof operator is used to determine if class or object is in the hierarchy – “is a” relation. | ||||||||||||||||||||||||||||||||
| Exam Tip | Interface provide an “is a” relation. | |||||||||||||||||||||||||||||||
| Features of Encapsulation | Modularity (data and methods)
Implementation hiding (hide data) (interface access)
Extensible (code reuse)
Code clarity (reduce code coupling) |
|||||||||||||||||||||||||||||||
| Defines “is a” and “has a” relation. | ||||||||||||||||||||||||||||||||
| An Encapsulated class allows programmer to change an implementation of the super class without affecting the parent class. | ||||||||||||||||||||||||||||||||
| Overloading | As part of OO Development a method may have the same name. | |||||||||||||||||||||||||||||||
| The return type is not enough for a method to be different. | ||||||||||||||||||||||||||||||||
| Exam Tip | Their parameter list must be different (data type and number of) | |||||||||||||||||||||||||||||||
| method call | The compiler is able to determine which method to call based on the parameter list. | |||||||||||||||||||||||||||||||
| Simple parameter list difference are easily identified on which method gets called. | ||||||||||||||||||||||||||||||||
| Different implentation | public class A {print()
print(int i) print(long i) } |
|||||||||||||||||||||||||||||||
| Calling print( 2 ) would call print( int i).Calling print() would call print() with no parameters. | ||||||||||||||||||||||||||||||||
| short x = 3;Calling print( x ) would call print( int i ). Casting occurs. | ||||||||||||||||||||||||||||||||
| Casting | x would be casted to an int before the call. The value is widen. | |||||||||||||||||||||||||||||||
| Exam Tip | The parameter list closest to the matching parameter list gets called. | |||||||||||||||||||||||||||||||
| Java would do to much work to cast to long. It takes the easiest route. | ||||||||||||||||||||||||||||||||
| Exam Tip | If the casting is to ambiguous the compiler would generate an error. The signature are not clear to the compiler on which method to choose. | |||||||||||||||||||||||||||||||
| Exam Tip | Overloaded method with identical parameter list never compile. | |||||||||||||||||||||||||||||||
| OverriddenClass implementation | As part of OO Development a method in derived class may have the same name as that of its base class but behave differently. | |||||||||||||||||||||||||||||||
| Exam Tip | The return type and parameter list and exception must be the same hierarchy or none | |||||||||||||||||||||||||||||||
| The compiler is able to determine which method to call based on the object on which the method is called. | ||||||||||||||||||||||||||||||||
| visibility | The overridden method may be made more public than the parent class method. | |||||||||||||||||||||||||||||||
| Exam Tip | Overridden subclasses method may not be more private less access restrictionA compiler error is generated if this rule is violated. | |||||||||||||||||||||||||||||||
| Exceptions | The exception thrown in the subclass’ method may be equal or a subclass exception or the super class exception for the super class, or none | |||||||||||||||||||||||||||||||
| Exam Tip | Overridden exception maybe fewer or none in the subclass | |||||||||||||||||||||||||||||||
| Exam Tip | Overridden exception must be in the same hierarchy as the base.They can not throw different hierarchy exception. | |||||||||||||||||||||||||||||||
| An overridden method does not inherit the exception of the base class. | ||||||||||||||||||||||||||||||||
| synchronize | The synchronize keyword works independent of overridden. | |||||||||||||||||||||||||||||||
| Polymorphism | In the scheme of inheritance since methods are defined in the base and subclass. Polymorphism allow subclasses to share the methods of the base class. | |||||||||||||||||||||||||||||||
| Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class. | ||||||||||||||||||||||||||||||||
| Overriding and Overloading allow polymorphism to be achieved. | ||||||||||||||||||||||||||||||||
| Organic method binding | Which method to call is determine via dynamic method biding. Method calls are determined at runtime. | |||||||||||||||||||||||||||||||
| Overriding and Method Access | public class A {print();
} public class B extends A { print(); } public class C { print() A a = new A(); B b = new B(); A a2 = new B(); } |
|||||||||||||||||||||||||||||||
| Reference a and b method calls are easy.
They call the methods referred to by new A() and new B(). |
||||||||||||||||||||||||||||||||
| A and B refer to their underlying reference type in memory. | ||||||||||||||||||||||||||||||||
| a2 is declared as type A, but refers to an object of type B. | ||||||||||||||||||||||||||||||||
| Exam Tip | Overridden methods obey, behave has, their underlying reference type. What they refer to in memory.
Not the declared type. |
|||||||||||||||||||||||||||||||
| a2 calls the method of B. | ||||||||||||||||||||||||||||||||
| Exam Tip | There is no way to call the super once a reference has been established.a, b or a2 super cannot be used at this point. | |||||||||||||||||||||||||||||||
| Overriding and Data Access | Class always use their declared data members. Declared data is used. | |||||||||||||||||||||||||||||||
| Exam Tip | How they are declared and not the object they refer define the data access.
Class refer to their data members. |
|||||||||||||||||||||||||||||||
| a2 would refer to A’s data. | ||||||||||||||||||||||||||||||||
| Overloading | Since overloading is defined within the class, no issue in determining data or method call. It’s within the class. | |||||||||||||||||||||||||||||||
| Constructor | ||||||||||||||||||||||||||||||||
| Default Constructor | If a constructor is not defined for a class.
The default constructor of the Object class is used – no argument constructor. |
|||||||||||||||||||||||||||||||
| Overloading | Both the base and derived class constructor can be overloaded. | |||||||||||||||||||||||||||||||
| this | The this keyword can be used to access other constructor in a class.
this(); this( 3 ); |
|||||||||||||||||||||||||||||||
| Exam Tip | this() must be the first statement called. | |||||||||||||||||||||||||||||||
| The same parameter list rules apply to this.They must match the constructor parameter list and casting may occur. | ||||||||||||||||||||||||||||||||
| super | Use the super keyword in a subclass to allow it to access the immediate super class constructor. | |||||||||||||||||||||||||||||||
| Exam Tip | super() must be the first statement called. | |||||||||||||||||||||||||||||||
| Exam Tip | In the subclass when no super keyword is given in the subclass constructor, the no parameter – super() – is automatically called. | |||||||||||||||||||||||||||||||
| For a subclass constructor declaration: | ||||||||||||||||||||||||||||||||
| Baseclass( String title ) {} | ||||||||||||||||||||||||||||||||
| Exam Tip | The sub class would call its parent class super() blank constructor if it exist.If it does exist, the code will not compile. | |||||||||||||||||||||||||||||||
| Clone | No operator can make direct copy of an object in memory.
To make a copy of an object use the Object class clone() method. |
|||||||||||||||||||||||||||||||
| Cloneable Interface | Not all classes can be cloned. They must implement the Cloneable interface. | |||||||||||||||||||||||||||||||
| The Cloneable interface has no methods but servers as a marker. | ||||||||||||||||||||||||||||||||
| If the instanceof operator returns true when an object implements the Cloneable interface it can be cloned. | ||||||||||||||||||||||||||||||||
| The CloneNotSupportedException is thrown when the Cloneable when an object can not be cloned. | ||||||||||||||||||||||||||||||||