Monday, 4 February 2013

INHERITANCE | Java J2EE Tutorial pdf

INHERITANCE

Inheritance, another fundamental concept of object-oriented programming. The idea behind inheritance is that we can create new classes that are built upon existing classes. When we inherit from an existing class, we reuse (or inherit) methods and fields, and we add new methods and fields to adapt our new class to new situations.
(The arrow in the above UML diagram points from the derived class to the base class. As we will see, there can be more than one derived class.)
We have two ways to differentiate our new derived class from the original base class. The first is: We simply add new functions to the derived class. These new functions are not part of the base class interface. The second and more important way to differentiate our new class is to change the behavior of an existing base-class function. This is referred to as overriding that function.
Here is how we define a Derived class that inherits from the Base class. We use the Java keyword extends to denote inheritance.
class Derived extends Base
{
added methods and fields
}

No comments: