Java Inheritance and Interfaces

Levels of Inheritance

3-Level Hierarchy

Java supports multi-level inheritance, where a class inherits from another class that inherits from a base class. For example:

class Animal {  void eat() {         System.out.println("Eating...");     }} class Dog extends Animal { void bark() {         System.out.println("Barking...");     }}  class Labrador extends Dog { void color() {         System.out.println("Labrador is golden in color.");     }} public class Main {     public static void main(String[] args) {         Labrador labrador = new Labrador();         labrador.eat();         labrador.bark();        labrador.color(); }}

Single-Level Inheritance

In single-level inheritance, a class inherits directly from a base class. For example:

class Animal { void eat() {         System.out.println("Animal is eating"); }} class Dog extends Animal {     void bark() { System.out.println("Dog is barking"); }} public class Main {     public static void main(String[] args) {         Dog dog = new Dog();         dog.eat(); dog.bark(); }}

Interfaces and Multiple Inheritance

Java doesn’t support multiple inheritance with classes directly, but it can be achieved using interfaces. Interfaces define method contracts that implementing classes must follow.

Defining and Implementing Interfaces

interface Interface1 { void method1(); } interface Interface2 { void method2(); } class MyClass implements Interface1, Interface2 { public void method1() { }  public void method2() { } } public class Main { public static void main(String[] args) {  MyClass obj = new MyClass();  obj.method1(); obj.method2(); }}

Benefits of Interfaces

  • Abstraction: Interfaces define method contracts without implementation details.
  • Multiple Inheritance: Classes can implement multiple interfaces.
  • Loose Coupling: Interfaces promote flexibility and maintainability.

Method Overriding

Method overriding allows a subclass to provide a specific implementation of a method inherited from its superclass.

class Animal { void makeSound() {         System.out.println("Animal makes a sound"); }} class Dog extends Animal { void makeSound() {         System.out.println("Dog barks"); }} public class Main {     public static void main(String[] args) {         Animal animal = new Animal();         animal.makeSound();        Dog dog = new Dog();         dog.makeSound(); }}

The `super` Keyword

The `super` keyword refers to the immediate parent class. It’s used to access overridden methods or constructors of the superclass.

class Animal { void sound() {         System.out.println("Animal makes a sound"); }} class Dog extends Animal { void sound() {         System.out.println("Dog barks"); }  void animalSound() {         super.sound(); }} public class Main {     public static void main(String[] args) {         Dog dog = new Dog();         dog.sound();                dog.animalSound(); }}

Abstract Classes

Abstract classes cannot be instantiated and may contain abstract methods without implementation. They serve as blueprints for subclasses.

abstract class Animal { abstract void makeSound();         void eat() {         System.out.println("Animal is eating"); }} class Dog extends Animal { void makeSound() { System.out.println("Dog barks"); }} public class Main {     public static void main(String[] args) { Dog dog = new Dog(); dog.makeSound();        dog.eat(); }}

Nesting Interfaces

Interfaces can be nested within other interfaces for logical grouping.

Interface Syntax

interface MyInterface {      int MY_CONSTANT = 10;        void method1();        void method2(int param);     default void defaultMethod() {  }     static void staticMethod() {  }}