Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
Inheritance is a key concept in object-oriented programming that allows you to create a new class by extending an existing one. The new class (subclass) inherits the properties (variables) and methods from the existing class (superclass). Inheritance promotes code reusability and makes it easier to create and maintain applications. In this tutorial, we'll discuss the basics of inheritance in Java and provide examples of its usage.
To create a subclass that inherits from a superclass, use the extends
keyword.
Example:
Superclass (Animal.java):
public class Animal { public void makeSound() { System.out.println("The animal makes a sound"); } }
Subclass (Dog.java):
public class Dog extends Animal { }
Main.java:
public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.makeSound(); // The animal makes a sound } }
In this example, we create a superclass called Animal
with a makeSound
method. We then create a subclass called Dog
that extends the Animal
class. The Dog
class inherits the properties and methods from the Animal
class, so we can call the makeSound
method on a Dog
object.
A subclass can override methods from the superclass to provide a new implementation. This is useful when you want to change or extend the behavior of a method in the subclass.
Example (continuing from the previous example):
Dog.java:
public class Dog extends Animal { @Override public void makeSound() { System.out.println("The dog barks"); } }
Main.java:
public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.makeSound(); // The dog barks } }
In this example, we override the makeSound
method in the Dog
class to provide a new implementation. Now, when we call the makeSound
method on a Dog
object, the new implementation is used.
super
keywordThe super
keyword can be used to call the superclass's constructor or methods. This is useful when you want to access the superclass's implementation of a method or initialize the superclass's properties.
Example (continuing from the previous examples):
Dog.java:
public class Dog extends Animal { @Override public void makeSound() { System.out.println("The dog barks"); super.makeSound(); } }
Main.java:
public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.makeSound(); // Output: // The dog barks // The animal makes a sound } }
In this example, we use the super
keyword in the Dog
class to call the superclass's makeSound
method. When we call the makeSound
method on a Dog
object, both the new implementation in the Dog
class and the original implementation in the Animal
class are executed.
In conclusion, inheritance is a fundamental concept in Java that allows you to create new classes by extending existing ones. This promotes code reusability and simplifies the process of creating and maintaining applications. By understanding the basics of inheritance in Java, you can leverage the power of object-oriented programming to create flexible and maintainable code.
Creating and extending classes in Java
// Superclass class Animal { void eat() { System.out.println("Animal is eating"); } } // Subclass extending Animal class Dog extends Animal { void bark() { System.out.println("Dog is barking"); } }
Overriding methods in Java inheritance
class Dog extends Animal { // Overriding the eat method @Override void eat() { System.out.println("Dog is eating"); } }
Access modifiers in Java inheritance
Access modifiers control the visibility of members in inheritance:
// Superclass with protected method class Animal { protected void eat() { System.out.println("Animal is eating"); } } // Subclass accessing the protected method class Dog extends Animal { void consume() { eat(); // Accessing the protected method } }
Polymorphism and dynamic method dispatch in Java
Animal animal = new Dog(); // Polymorphism animal.eat(); // Dynamic method dispatch
Abstract classes and methods in Java inheritance
// Abstract superclass abstract class Shape { abstract void draw(); } // Concrete subclass implementing abstract method class Circle extends Shape { @Override void draw() { System.out.println("Drawing a circle"); } }
Interfaces and multiple inheritance in Java
// Interface interface Walkable { void walk(); } // Class implementing an interface class Human implements Walkable { @Override public void walk() { System.out.println("Human is walking"); } }
Composition vs. inheritance in Java
Composition is an alternative to inheritance for reusing code:
// Composition example class Car { Engine engine; // Composition } class Engine { void start() { System.out.println("Engine started"); } }