View RSS Feed

Java Object

Object-oriented programming in Java.

  1. Consider composition instead of subclassing

    by , 04-26-2012 at 07:16 PM
    Composition might be considered as subclassingís alternative.

    Composition :
    • is simply implemented by all call forwarding to object field.
    • consists of zero dependence upon the object fieldís implementation details.
    • is considerably much more flexible, as it is dynamically defined at the runtime instead of compile time

    Subclassing issues are :

    • Encapsulation is violated since the implementation of subclass & superclass gets tightly
  2. Composition and Aggregation using Java

    by , 04-26-2012 at 07:15 PM
    Always, there exist a confusion b/w aggregation & composition. In case of a practical field of aggregation and compostion, it shall be kept in minf that both of them are of association type. Below is given the definition of term. After that practical java code is given so that to exactly understand that how aggregation and composition implementation could be implemented in programming.

    Objectís life time is considered to be the same. For instance, composition ...
  3. Choosing composition vs. inheritance

    by , 04-26-2012 at 07:14 PM
    Both inheritance and composition permits you to put the sub-objects in new class. One may be amazed regarding the difference present b/w two and when one could be chooses over the other.

    Generally, composition is used if an existing class features are required in your new class but not the interface. This means that an object is embedded to use it for implementation of functionality of your new class. However, your new class user may see the interface that has been defined instead ...
  4. Composition

    by , 04-26-2012 at 07:13 PM
    In object oriented design when progress is made, various objects will be encountered in problem domain which holds many other objects. In such cases, one will need to model a same kind of arrangement, in your solutionís design. In case of Java programís object oriented design, the manner in which objects are modeled that have other objects, is with composition. Constituent object references become the containing objectís field, with composition.

    For instance it is considered to be ...
    Tags: composition Add / Edit Tags
  5. 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 ...
  6. 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.


    • 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,
  7. 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.
  8. 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Ē.
  9. Abstract Methods

    by , 04-26-2012 at 07:01 PM
    If you desire a particular method to be present in a class and actual methodís implementation be determined by the help of a child class then you must be declaring methods which are present in parent class, as an abstract.
    A method to be declard as an abstract, abstract keyword is being used. There is no method body in abstract method but do have a method signature.
    Signatures of abstract methods are followed by semicolon and they have no definition:
    Java Code:
    public abstract
  10. Java - Abstraction

    by , 04-26-2012 at 07:00 PM
    Abstraction means an ability to create the class abstract, in OOP. Basically, an abstract class is not able to get instantiated. All remaining class functionalities are still present and its methods, fields & constructors are accessible in similar way. Just abstract classís instance canít be created.

    If it is not possible to instantiate a class and it is abstract then such kind of class has no particular uses, until it gets subclasses. Typically, in this way abstract class will ...
  11. Polymorphism

    by , 04-26-2012 at 06:57 PM
    Polymorphism is considered as an objectís ability so that it could take on various forms. It is most commonly used in OOP where parent class reference usually refers the child class objects.

    Any java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
    It shall be known that objects are access by one single way and that is ...
    Tags: polymorphism Add / Edit Tags
  12. Rules for method overriding

    by , 04-26-2012 at 06:56 PM
    ē Overridden method and argument list shall exactly be the same
    ē Return type shall be the subtype that is being declared in original method that is overridden in superclass, or it shall be the same.
    ē Restriction is not more in case of access level as compared to access level of the overridden method. E.g, when superclass is considerd to be public, in that case overriding method present in the subclass would not be public or private. But access level might not be much restrictive ...
  13. 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 ...
  14. Overriding with constructors

    by , 04-26-2012 at 06:53 PM
    • A constructor canít be overridden in a superclass, because they are non-inherited.
    • A constructor canít be overridden in similar class because both of them would have similar signatures and compiler error would be there.
    • When a subclass object is instantiated & a method is called by superclass constructor which in superclass gets overridden, superclass constructor will be calling the subclass methods.

    Java Code:
    class Super {
  15. Java method Overriding

    by , 04-26-2012 at 06:42 PM
    Example that has been given below shows the method overriding, in Java. In java, method overriding is a subclass method that overrides the super class method. Extend keywords are used by the subclass for extension of the super class. In the example, Class B is present in the subclass. Class A is present in super class. Overriding method of superclass & subclass has similar signatures. Methods of the superclass are being modified by using overriding.
    Java Code:
    class A {
    int i;
  16. Overloading Methods

    by , 04-26-2012 at 06:41 PM
    Overloading methods are supported by the Java programming language and methods could be distinguished by Java using various method signatures. Within class, methods might be having similar name when different lists of parameters are present.

    Let us assume that a class is there which uses calligraphy for different data type drawing and which have a method to draw each type of data. New names are used for every method, like drawString, drawFloat and drawInteger & so on. Same name ...
  17. What is overloading in java

    by , 04-26-2012 at 06:40 PM
    Two or more than two methods could be defined in Java, within similar class which has similar names with different parameter declarations. In this particular case, methods are overloaded and this is known as method overloading. Method overloading is method by which polymorphism is implemented.

    Sample program of overloading:
    Java Code:
    class Overloading {
    void test() {
    System.out.println("No parameters");
    void test(int a, int b) {
  18. Basics of Method Invocation

    by , 04-26-2012 at 06:39 PM
    Let us revive the method invocation features present in JVM. To invoke a method new frame is made at the execution stack. Every frame consists of oerand stack and local variable array. After the frame creation, operand stack gets emptied & the local variable gets quite populated, along with this which is the target object, and arguments of method. On operand stack, all processing occurs. Maximum stack slots and local variable numbers, that are used while the invocation of method shall be known, ...
  19. Overloading on return values

    by , 04-26-2012 at 06:39 PM
    It is commonly wondered that why method argument & class names lists? Why methods shall not be distinguished ,based at return values? E.g, two methods having similar arguments and name get distinguished quite easily from each other:

    void f() {}
    int f() {}

    It will be working fine if unequivocally a compiler finds out the meaning, as in int x = f( ). But, a method could be called and return value can be ignored; which is referred as method calling for sideeffects, ...
  20. Class Methods

    by , 04-26-2012 at 06:38 PM
    If class method is defined by the subclass with similar sign as that of class method which is present in superclass, then in subclass the method present will be hiding the one which is present in superclass.
    Distinction b/w overriding and hiding possess certain important implication. Overridden methodís version which is invoked is that one which is present in subclass. Hidden methodís version which is invoked is totally dependent upon whether it gets invoked by the subclass or superclass. ...
  21. Instance Methods

    by , 04-26-2012 at 06:36 PM
    In a subclass, an instance method with same sign (name, number & parameter type) & return type, as instance method overrides method of superclass.

    Subclass has an ability to override method which permits class for inheritance, from superclass. Superclass has a behavior which is considered to be close enough and then behavior is modified as per requirement. Overriding method consist of similar parameter types, return type, number and name, as method that it overrides. Typeís ...
  22. Casting Objects

    by , 04-26-2012 at 06:36 PM
    As it has been shown that object is classís data type, from which instantiation took place. If we write, for example:
    Java Code:
    public MountainBike myBike = new MountainBike();
    myBike is of type, MountainBike.

    MountainBike get descended from Object & Bicycle. Hence, MountainBike is an object as well as a Bicycle. It is used when Object or Bicycle objects are called.

    It is not necessary that reverse is true: Bicycle might be MountainBike, however ...
  23. Private Members in a Superclass

    by , 04-26-2012 at 06:34 PM
    Private members are not inherited by the subclass, of the parent class. But in case a superclass has protected or public methods to access the private fields, subclass may use them.

    Nested class holds access to enclosing classís private members-both methods & fields. Hence protected or public nested class which has been inherited by subclass holds access of superclassís private members indirectly.
  24. The instanceof Keyword

    by , 04-26-2012 at 06:33 PM
    Instanceof operator can be used for determining that dog is an animal & Mammal is an animal.
    interface Animal{}
    Java Code:
    class Mammal implements Animal{}
    class Dog extends Mammal{
       public static void main(String args[]){
          Mammal m = new Mammal();
          Dog d = new Dog();
          System.out.println(m instanceof Animal);
          System.out.println(d instanceof Mammal);
          System.out.println(d instanceof
  25. HAS-A relationship

    by , 04-26-2012 at 06:32 PM
    Such relationships are usage based. It shows whether a class consist of HAS-A, certain thing. Code duplication and bugs are reduced by this relationship.

    For example:
    Java Code:
    public class Vehicle{}
    public class Speed{}
    public class Van extends Vehicle{
    	private Speed sp;
    Class Van HAS-A speed is shown. Speedís separate class doesnít require putting the complete code which has relation with speed, inside the Van class, that makes ...
  26. IS-A Relationship

    by , 04-26-2012 at 06:31 PM
    IS-A means this object is that objectís type. Now see, how inheritance is achieved by extends keyword
    Java Code:
    public class Animal{
    public class Mammal extends Animal{
    public class Reptile extends Animal{
    public class Dog extends Mammal{
    Following are true for the Object Oriented terms, based at above example:
    • Mammal classís superclass is animal.
    • Reptile classís superclass is
  27. What You Can Do in a Subclass

    by , 04-26-2012 at 06:29 PM
    Subclass inherits protected and public members of parent no matter in which package is the subclass. If subclass and parent are in same package then package-private members are also inherited. Inherited members could be used to hide them, replace them or supplement them, with newer members:

    • Just the way other fields do, inherited fields can also be directly used.
    • A field can be declared in subclass with similar name just like that present in superclass hence hiding it (which
    Tags: subclass Add / Edit Tags
  28. What Is Inheritance?

    by , 04-26-2012 at 06:28 PM
    Various objects consist of certain common amount with each other. Tandem bikes, mountain bikes and road bikes etc share the common bicycle characteristics (current gear, current speed, and current pedal cadence). Yet, additional features are defined byeach which will make them a bit different: Road cycles consist of handlebars; tandem bicycles consist of 2 handlebar sets & 2 seats; mountain bikes have an extra chain ring that provides low gear ratio to them.

    Object oriented programming ...
  29. Annotation Processing

    by , 04-26-2012 at 06:27 PM
    Uses of advanced annotation comprises of the annotation processor writing that reads the Java program. Actions are taken based at the annotations. Generation of the auxiliary source code takes place that would relieve programmer to create the boilerplate code,that will be following the predictable patterns. 5 of JDK will be released to facilitate this task, which will be including annotation processing tool. When 6 of JDK is released, functionality of apt is the standard part of java compiler. ...
  30. Annotations

    by , 04-26-2012 at 06:26 PM
    Annotations provide that data regarding program which is not programís part. No direct effect they have at the code operation, they annotate.

    Uses of the annotations are as following:

    • Information for compiler: Annotations can be used by the compiler for detection of the errors or suppress warning.
    • Deployment and compiler-time processing: Software tools can process the annotation information so that to generate the code, XML files or so forth.
    • Runtime processing:
    Tags: annotations Add / Edit Tags
Page 1 of 3 123 LastLast