Java – Object Oriented Programming – part 2

 



Object-Oriented Programming



 

Part 2

1. Code Block

There are three types of code block: Local Code Block, Static Code Block, and Constructor Code Block.

Code Blocks serve varying purposes.
Starting from local code blocks, local code blocks are associated with the period of duration for local variables, functioning only within the main method.

Static Code Blocks exist in classes. The loading process of a static code block proceeds with that of the class, playing a role in initialization. Please beware that static code blocks are executed only once.

Constructor Code Blocks also exist in classes. The difference between Constructor Code Blocks and Static Code Blocks are their period of duration – every time a Constructor Method is executed, the Constructor Code Block will also be launched, and this process takes place even prior to Constructor Method.
Order of Execution:

Static Code Block —> Constructor Code Block –> Local Code Block

2. Extends

The Java Keyword extends provides us with a tool that links different class while preserving the variables and functions within each original class. When Keyword extends is activated, there are always two parties involved: the class being extended , which we define as a “parent class”, and the class that extends, which we speak of as a“child class”.

Advantages associated with extends include:

A. allowing repetitive use of codes

B. incurring easy Maintenance

C. linking class from one to another, which also serves as a foundation for polymorphism to take place (to be discussed in next chapter).

Keyword extends works in a way that allows for extention from one class to another. It doesn’t, however, allow extension from one class to multiple classes. This is somewhat different from other coding languages including C++ and C.
However, although extensions into multilateral classes are forbidden, Java does allow extension that flows through an entire line of classes. For example, B class extends A class; C class can also extend B class with the original variables and functions inherited to C(in which case barring override)

With extends, you can also have one class’s Constructor Function replicated to another class. However, the rules for such extension are a bit complicated. The three following principles may serve as a guideline when you try to use extends.

1) In default, the child class will extend the parent class’s Non-Parameter Constructor.

2) If the parent class doesn’t have a non-parameter constructor, the compiling process will not proceed. In the light of this situation, there are three ways we may follow to address it:

i. Provide the parent class with a non-parameter constructor.

ii. Use the keyword Super to call for parent class’s parameter constructor.

iii. Use super(…) to request access to parent class’s constructor indirectly.

3) In the case that parameters of the child class share a same name with that of a parent class, and that the name of this parameter is being referenced, the reference order follow the following pattern:

i. The system tries to identify whether such a parameter is a local variable for the child class. If not, proceed to the next step.

ii. The system tries to search for this paramater among the member variables of this class. If nothing is found, proceed to the next step.

iii. The system tries to find this parameter among local variables of the parent class’s functions. If nothing is found, proceed to the next step.

iiii. Finally, the system tries to locate this parameter upon the father class’s member variables. If still nothing is found, compiling will fail.

3. Override and overload

Differences :
Overload takes place when one class contains two ore more functions that share the same name yet different parameters. It has nothing to do with returned values.

Override takes effect when a child class has a function that is similarly with that of its parent class not only in terms of name/title, but also returned values and parameters.




Sites DOT MIISThe Middlebury Institute site network.