View RSS Feed


Java Inheritence

  1. 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. ...
  2. 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 ...
  3. 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 ...
  4. 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.
  5. 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
  6. 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 ...
  7. 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
  8. 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
  9. 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 ...
  10. Using Inheritance - Substitution vs. Extension

    by , 11-30-2011 at 03:51 AM
    We are going to look at inheritance hierarchy. If we take a classic approach, as shown below we would see the methods that have been defined in the base class overridden in the derived classes. Inheritance guarantees that any derived class will have the interface of the base class. If you follow this diagram below, derived classes will also have no more than the base-class interface.

    Name:  Inheritance-Example.jpg
Views: 1037
Size:  16.7 KB

    This can be thought of as pure substitution, because ...
  11. Design Principle: Choose Composition vs. Inheritance

    by , 11-30-2011 at 03:48 AM
    With composition or inheritance you can place sub-objects inside your new class. If you do this explicitly than you are using composition otherwise if you do this implicitly, you are using inheritance. I will outline the difference between the two below as well as point out one of the key design patterns to use in your development. Prefer composition vs inheritance.

    Composition is used when you want the features of an existing class to be included inside your new class, but not be ...
  12. Composition Syntax for Reuse

    by , 11-29-2011 at 06:21 PM
    One of the best features of Java is code reuse. But like any tool, it needs to be used effectively. Most programmers might be lead to think that when I talk of reuse only relates to extending a class. Now there is nothing wrong with reusing a class by extending it or using it directly. This type of reuse is using the class without the risk changing the existing code. First we can create objects of the existing class inside the new class. We call this composition. It is a new class composed of objects ...
  13. Composition Rather than Inheritance

    by , 11-29-2011 at 02:27 AM
    One of the more abused features of object oriented languages is inheritance. Second on the list of abused features of Java as an outgrowth of inheritance is the use of polymorphism. Developers seem to believe that everything should be inherited, and that classes should use polymorphism for every object irrespective of whether the arguments will be provided from external sources. Doing these sorts of things will make your designs more difficult to understand and lead to complications later when ...