Witscale Test Center

Chapter summary


Chapter summary

This chapter covers a lot of ground. We learned how encapsulation helps in writing robust and highly maintainable code. We also learned about object-oriented design and implementation techniques with IS-A and HAS-A relationships. Then we turned our focus on the code reuse techniques. In method overloading, we learned how method name is reused whereas in method overriding we saw how we can modify the few method definitions while keeping the rest of the inherited behavior. We also learned in detail, the rules of method-name overloading and overriding. Finally we turned our attension to the constructors. We learned how the constructors can be overloaded similar to the method and also how to call them using this and super. Then we saw how and when the compiler automatically creates call to the zero-argument constructor of superclass.  Specifically, this chapter covers a part of the sixth SCJP objective, which expects you to know two basic feature of OO, encapsulation (6.1) and inheritance (6.2) and also the benefits of implementing these features in your Java code. This is brief summary of the key concepts learned in this chapter.

 

    Encapsulation

§          The main reason behind encapsulation is to hide the implementation details behind the programming interface (public methods) of a class.

§          A class with tight encapsulation has following features –

§          All instance variables are kept well protected usually by declaring them as private. Accessor methods are provided to access the instance variables.

§          Services provided by the class are declared as public methods and all internal methods are declared as private.

 

    The Generalization (Inheritance) and IS-A relationship

§          Generalization is the result of abstracting the common attributes and behavior from a group of closely related classes and moving them into a common super class. Specialization is the inverse of generalization.

§          A common test for generalization is the IS-A test. Generalization may also look for following phrases in problem definition to identify the inheritance.

§          "is a"

§          "is a type of"

§          "is a kind of"

 

§          If part of problem definition says ‘Dog IS AN animal’, implementation will have an Animal class and Dog class will be the subclass of the Animal class. Therefore IS-A relationship marks the inheritance among classes.

 

    The Composition and HAS-A relationship

§          Composition is the relationship between a class and its constituent parts.

§          Composition passes the “HAS-A” test. Composition may also look for following phrases in problem definition to identify the composition.

§          "has a"

§          "is made up of"

§          "contains"

§          If part of problem definition says ‘An Automobile HAS-A engine’, implementation will have an Automobile class with engine as member variable.

 

       Method Overloading

§          If two (or more) methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but different signatures, then the method name is said to be overloaded.

§          The signature of a method consists of the name of the method and the number and types of formal parameters in particular order. If a class declares two methods with the exact same signature, a compile-time error occurs.

§          Only the method name is reused in overloading, so method overloading is actually method name overloading.

§          Overloaded methods may have arguments with different types and the order of the arguments may be different.

§          Overloaded methods are not required to have the same return type. They also do not have any restriction on the exceptions they can throw.

§          Overloading is particularly used while implementing several methods that implement similar behavior but for data sets of different types.

§          Overloaded methods are independent methods of a class and can call each other just like any other method.

 

       Method overriding

§          When a class defines a method with same method name, argument types, argument order and return type as a method in its super class, its called method overriding.

§          The method in the superclass is said to be overridden by the method in the subclass.

§          Overriding method actually replaces the behavior in super class for a subclass.

§          Rules for overriding

§          Methods overriding cannot be declared more restrictive than the super class method. The access modifiers are public, protected, default and private from least restrictive to most restrictive.

§          Any exceptions declared in overriding method must be of the same type or any of its subclass types as those thrown by the overridden method.

§          Methods declared as final cannot be overridden.

§          An overriding method can be declared as final as the keyword final only suggests that this method cannot be further overridden.

§          Methods declared as private are not overridden as they are not visible outside the class.

§          Overriding method can call the overridden method (just like any other method) in its super class with keyword super.

§          The call super.method() will invoke the method of immediate super class.

§          The keyword super is used to refer to only the immediate super class. Therefore the method call super.super.method() is invalid.

 

       Summary of the differences between overloading and overriding

 

Overloading methods

Overriding method

Overloaded methods supplement each other.

An overriding method replaces the method it overrides.

 

Overloading methods may have different argument lists of different types.

 

An overriding method must have argument list of identical type and order as the overridden method.

The return type may be different for overloaded methods

 

The return type must be same as that of overridden method.

 

Since overloading methods are essentially different methods, they can throw exceptions without any restriction.

 

The overriding method must not throw checked exceptions that cannot be thrown by the original method.

Since overloading methods are essentially different methods, they can have any of the access modifiers.

 

The overriding method must not have a more restrictive access modifier than that of the overridden method.

 

       Constructor Overloading

§          Constructors can also be overloaded in similar fashion, as they are also methods.

§          One constructor can call another overloaded constructor of the same class by using this keyword.

§          One constructor can call constructor of its super class by using the super keyword.

 

 

       Methods are overridden, but the variables are shadowed.

 

§          A subclass can have a variable with the same name as a variable in the superclass. This is called as shadowing the superclass variable. It is not overriding of variable. When a member variable is accessed through an object reference, the variable to be selected depends on the declared type of the object reference variable.

 

§          A subclass can also have a method with the same signature as a method in the superclass. This is called as overriding the superclass method. If such method is called, the actual method to invoke is determined by the type of actual object on which the method is called and not on the type of variable holding the object reference.