| Inheritance - An Overview | |||||||||||
| What is Inheritance? | Forms of Inheritance | ||||||||||
| Why use Inheritance? | Kinds of Inheritance | ||||||||||
| Home | |||||||||||
| Kinds of Inheritance
In Java inheritance is used for two main purposes: 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. For class inheritance, Java uses the keyword extends to inherit the base class. public class derived-class-name extends base-class-name { // derived class methods extend and possibly override // those of the base class } 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 the interface. For interface inheritance, Java uses the keyword implements to inherit the base class public class class-name implements interface-name { // class provides an implementation for the methods // as specified by the interface } |
|||||||||||