View RSS Feed

Encapsulation

Java Encapsulation

  1. Tagging Interfaces

    by , 04-26-2012 at 07:12 PM
    Extending interfaces use which is very common occurs usually when no methods are present in the parent interface. E.g, in java.awt.event package the MouseListener interface extended the java.util.EventListener, which will be defined as following:
    Java Code:
    package java.util;
    public interface EventListener
    {}
    Interfaces that haven’t any methods present are the tagging interfaces. Tagging interfaces have two main design purposes:

    A common parent is ...
  2. Self-encapsulate fields

    by , 04-26-2012 at 07:11 PM
    Within class implementation, this is a coomon thing to refer to the private fields directly. Another style present where all of the internal references present are indirect & further proceedings is done via set & get methods. This is what is known as self encapsulation.

    Self-Encapsulation:

    • is required to do the lazy initialization
    • is more difficult for reading as compared to the direct field access
    • Might be able to develop the synchronization policies,
    ...
  3. Benefits of Encapsulation

    by , 04-26-2012 at 07:10 PM
    • A class’s field might be made write only or read only.
    • A class might be having total access or control at what has been present in fields.
    • Class users are not aware of the fact that how to store data in class. Class might be changing the field’s data type, & class users don’t require any change in their code.
  4. Java - Encapsulation

    by , 04-26-2012 at 07:09 PM
    Among four fundamentals of OOP concepts, encapsulation is the important one. Other three included are abstraction, polymorphism and inheritance.

    Encapsulation is basically a field making technique in class private which also provides an access to the fields through public methods. When fields have been declared private then no one presnt outside of the class has access, thereby fields are hided in class. This is the reason encapsulation is given another name “data hiding”.
    ...
  5. What is overriding

    by , 04-26-2012 at 06:54 PM
    A method is sometimes inherited by a subclass from superclass which is not according the requirements. Perhaps, twenty methods are inherited by subclass & one among them is not right.
    Subclass in such case will be overriding those methods, by method redefinition.
    Method that is defined in superclass is not affected by this override.

    In following example, Cat class is the subclass & superclass includes Animal. Eat() method that has been inherited by the Animal ...