Sunday, September 23, 2012

Java tutorials



·         Inheritance is the programming technique where new classes are derived from existing classes by “inheriting” data fields and methods from those existing classes.
·         For example, classes to model circles and rectangles could inherit some data fields and methods from a general object for geometric objects. 
·         The general geometric object might include methods and data fields that would be common to all geometric objects in general, like the color to use when drawing the object.  These methods would be inherited by the circle and rectangle classes, because they both need a color choice for graphing.
·         Additional data fields like a radius for a circle object and side lengths for a rectangle object would be specified in those classes, because that is additional info particular to those objects, and are not generally applicable to any geometric object.
·         Additional methods like computing the area and perimeter would be in the rectangle and circle classes only, because those calculations are not the same for any general geometric object.
·         Adding subclasses to a given class is called extending the given class.
·         All the classes we have used or implemented ourselves up to this point implicitly extended the java.lang.Object class.
·         In fact, the structure of the Java language is that all classes must either implicitly or explicitly extend the class java.lang.Object.


Superclasses and Subclasses
·                     In Java terminology, if a class C1 is extended from another class C2, then the class C1 is called the subclass, and the class C2 is called the superclass.
·                     Synonyms for superclass: parent class, base class
·                     Synonyms for subclass: child class, extended class, derived class.
·                     A subclass inherits accessible data and methods from its superclass, and may also add new data fields and methods.
·                     Subclasses are denoted in UML diagrams with an arrow pointing from the subclass to the superclass.
·                     To declare that a class is a child of a superclass, use the extends keyword when implementing the class:
class SubclassName extends SuperclassName
{
     //new data fields
     //constructors
     //new methods
     //overridden methods
     //overloaded methods
}

Some design concepts worth considering when working with inheritance:
·                     A subclass is not a subset of its superclass.  In fact, a subclass usually contains more information and methods than its superclass.
·                     Inheritance is used to model the “is – a” relationship. 
o   For example, a circle “is a” geometric object. 
o   Do not blindly extend a class just for the sake of reusing methods.  For example, do not have a Tree class extend a Person class just to use methods like getHeight() and getWeight() that might be common to both classes.
·                     Only “is – a” relationships where the subclass contains more detailed information than the superclass should be modeled using inheritance. 
o   For example, even though a rhombus “is a” parallelogram, a rhombus does not contain any more detailed information than a parallelogram does, and so a Rhombus class should extend a geometric object class (and not a Parallelogram class).




Overriding Methods
When overriding a method of a superclass:
·                     Supply a different implementation of a method that exists in the superclass
·                     Must have same signature (same name and same parameter types)
·                     If a method is applied to an object of the subclass type, the overriding method is executed
·                     Private data fields in a superclass are not accessible to subclasses.  They can only be accessed through accessor/mutator methods of the superclass.
·                     Private methods in the superclass can not be overridden, because only accessible methods can be overridden.
·                     Static methods in the superclass can not be overridden.  If you try, then the static method in the superclass is hidden, and can only be accessed by calling the static method of the superclass explicitly.
·                     Data fields in the superclass can not be overridden.


Overriding vs. Overloading
To overload a method of the superclass:
·                     Supply a different implementation (with a new signature) of a method that exists in the superclass.  (Same as before…)
·                     Overridden methods have the same signature (list of parameters) and same return type.
·                     Overloaded methods have a different signature or different return type.

 


 
 

No comments:

Post a Comment