Reusability in Java – Inheritance..

Inheritance is a compile-time mechanism in Java that allows you to extend a class (called the base
class or superclass) with another class (called the derived class or subclass). In Java,
inheritance is used for two purposes:

1. class inheritance – create a new class as an extension of another class, primarily for the purpose
of code reuse. That is, the derived class inherits the public methods and public data of the
base class. Java only allows a class to have one immediate base class, i.e., single class
inheritance.
2. interface inheritance – create a new class to implement the methods defined as part of an
interface for the purpose of subtyping. That is a class that implements an interface “conforms
to” (or is constrained by the type of) the interface. Java supports multiple interface inheritance.

In Java, these two kinds of inheritance are made distinct by using different language syntax. For
class inheritance, Java uses the keyword extends and for interface inheritance Java uses the
keyword implements.
public class derived-class-name extends base-class-name {
// derived class methods extend and possibly override
// those of the base class
}
public class class-name implements interface-name {
// class provides an implementation for the methods
// as specified by the interface
}

Example of class inheritance

package MyPackage;

class Base {
private int x;
public int f() { … }
protected int g() { … }
}
class Derived extends Base {
private int y;
public int f() { /* new implementation for Base.f() */ }
public void h() { y = g(); … }
}

Example of interface inheritance

package MyPackage;

interface Interface_name {
private int x;
public int f();
protected int g();
}
class Derived implements Interface_name {
private int y;
public int f() { /* new implementation for Base.f() */ }
public void h() { y = g(); … }
}

Order of Construction under Inheritance

When you construct an object, the default base class constructor is called implicitly, before
the body of the derived class constructor is executed. So, objects are constructed top-down under
inheritance.

Since every object inherits from the Object class, the Object() constructor is always
called implicitly. However, you can call a superclass constructor explicitly using the builtin super
keyword, as long as it is the first statement in a constructor.

Advertisements

2 thoughts on “Reusability in Java – Inheritance..

    • Well, We are working on a string search algorithm currently. And the algorithm supports case-sensitive as well as case-insensitive string search. So basically, we have a class for string search, which is extended for the classes for pattern matching and finding mismatches and some other constraints. I hope I could help you understand more.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s