learningbydeveloping.com

 

Learning by developing

...because practice teaches you

PDF version: scjp T-2

T-2. Object orientation.

File class: name of public class(only one)

Develop code that implements tight encapsulation, loose coupling and high cohesion in classes.

Encapsulation: To access the variables between getters/setters and not directly, avoiding anomalous values.

Coupling: If one class A knows about B: It should know just about B’s methods:less coupling or loosely coupled... If A can access B’s variables, it means that both clases are coupled(more coupling).

Cohesion: Grade in which 1 class has been designed for doing 1 and just 1 objective, so that it is not doing anything not related to its job. So it is better to have a lot of different classes that share the job and not just one that does everything.

Polymorphism(many forms): You can treat any class of one class(gameshape) as an instance of that class(gameshape). (‘Is-a’ test): It says that “this thing is a type of that thing”. E.g.: Mustang is a type of horse so in OO terms we can say “Mustang IS-A Horse”. In Java this relation is defined through the keywords “extends”(for class inheritance) and “implements”(for interface implementations).

  • Downcasting: reference variable refers to a subtype(casting is needed). E.g.: ((B)a).method().

  • Upcasting: Reference variable refers to a supertype(casting NOT needed, but restrict capabilities of the object).

Casting: If you try to cast B to A: (A)B, it will throw a runtime exception(execution time) saying “java.lang.ClassCastException”. The compiler only checks that you do not do “upcasting” (you cannot cast ‘A’ to ‘B’ like: (B)A).

Note: b instance of B, if b is an object of B’s class(even if it is inside a superclass) it would return true. Additionally, you can ask if null is instance of SomeClass, it would return false.

E.g.: B extends A. A a = new B(); –> -execute B methods and use A variables.

–> But since it is an ‘A’ instance, then there will be only A’s methods available,so if you try to call a method that only exists in ‘B’ and not in ‘A’, it will throw a compilation error “cannot find symbol”. If you want to execute a (B) method, you can cast the object – downcasting – ((B)a).method() and call the method.

Important: This does not apply to static(Class) methods, and so it would call static methods from ‘A’: A a = new B(); <– a.staticMethod(); <– static method from A.

Constructor rules:

  • Any access modifier

  • Same name as class and no return value

  • There can be other methods with return value and same name as class but they will be methods, not constructors.

  • If not indicated, they are created by default, but if there is at least one constructor, it will not.They should call the constructor of the superclass(super() is called implicitly or with arguments,which should be called explicitly) and, optionally, its own constructor without arguments(or another one, through “this()” <– maybe this one executes “super()”), at the beginning of the method.

  • It is NOT possible to access any attribute/method from the instance until the superclass’s(super()) constructor has been executed. Only “static” mthods could be executed.

  • We can only execute one constructor from another constructor(not from methods).

Constructors calls:

 

Execution:

The second part is basically the execution order:

1st. Specific’s constructor is called and it calls Generic’s constructor.

2nd. Generic’s constructor is called and it calls Object’s constructor.

3rd. Instance variables from Object’s class take their explicit values.

4th. Object’s constructor is executed.

5th. Instance variables from Generic’s class take their explicit values.

6th. Generic’s constructor is executed.

7th. Instance variables from Specific’s class take their explicit values.

8th. Specific’s constructor is executed.

Invocation order: 1st.Specific –> 2º. Generic –> 3º.Objet

Execution order: 1st. Objet, 2nd Generic, 3º Specific.

Has-a: Has an object of type…

Overriding methods and Overloading: You can override methods with identical input/output through @Override before the method definition. This can be overrided, for instance, if the method of the inherited class is NOT final. Rules:-

  • You cannot override any static/final method.

  • In the same package: A subclass can override any method but “private” or “final”.

  • In different packages: A subclass can override any method from the superclass which is not final(can be private).

Note: You cannot override 1 final method BUT, if t he final method is private, it won’t be inherited and so it can be written again in the subclass.

  • Protected methods are inherited by subclasses

  • The Access level cannot be more restrictive than the overriden method(but can be less restrictive).

  • Can invoke superclass overriden methods with super.method(); Note: With polymorphism the superclass method will be called wether or not the overriden method is called.

Overriding Exceptions:

  • A subclass can add/remove runtime exceptions. It can also remove compilation time exceptions(but only remove, not add more). In this sense, if A extends B, and A’s method throws an exception, B MUST throw that exception, if not: compiler error.So it can throw less exceptions but not more than the overriden method. Summary:

Case 1: Overriding method throws runtime exception –Allowed

Note: So the new method can throw any runtime exception(but not compilation time exceptions).
Case 2:Overriding method
throws subclass of the exception type declared by the parent method – Allowed
Case 3: Overriding method
throws base class of exception type declared by the parent method( throws A, when parent throws B extends A) – NOT Allowed
Case 4: Overriding method
throws totally unrelated compile time exception replacing parent method throws clause-NOT Allowed

Overriding Attributes:

You can “override” in a subclass an attribute setting from normal to static the attribute, but this does not really override the attribute. It hides it. When the attribute is hidden, the original methods(from the parent class) refering the attribute will still sue the “normal” attribute, while the methods in the subclass will use the static one.

Overloading: Reusing a method name with different arguments(it is a different method)

E.g.: D extends C. And B extend A.

A has one method that returns C.

B has the same method that returns D.

This is correct, since B’s method returns a subclass of C.

Static methods:

  • Do not need any instance to access them.

  • They cannot access any non-static member from the class(main is static).

  • You cannot override one method but overloading(it is a different method with same name but different arguments).

Static variables:

  • Valid for a class so that all the objects/instances of that class share the SAME variable.

  • This kind of variables can be accessed without an instance of the class: Class.variable.

  • They live as long as the class’ objects live.

  • They can be imported “import class.valuestatic;”

  • They are initialized by default in the same way as “class variables”.

Note: Static variables are NOT serialized(you are serializing the object) when writing a file.

Important: If you use the name of the class, then you access the static attribute, but also through the object.attribute(even if it is a Class -static attribute), though this is a bad practice and it’s better to access through Class.attribute.

Return values from methods:

  • You can always return “null” if you were returning an “object”.

  • You can return an array if you need.

  • You can return a value(for primitives) or object, which can be “implicitly” casted. So if it is an Integer, you can return ‘1’ and if it is an object, you can return a subclass(B exteds A, the method returns ‘A’ but you return ‘B’).

**********************************************

The books I have used are these ones, which I strongly recommend. You can support this blog buying them through this links:

 

and

 

For OCJP version 7, I would recommend:

 

 

Categories: Java

Leave a Reply