Introduction
A class can extend another class and can implement one and more than one Java interface. Also, this topic has a major influence on the concept of Java and Multiple Inheritance.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | //Define an interface interface Animal { void eat(); void sound(); } // Cat class implements the Animal interface class Cat implements Animal { public void eat() { System.out.println("The cat eats food."); } public void sound() { System.out.println("The cat meows."); } } // Dog class implements the Animal interface class Dog implements Animal { public void eat() { System.out.println("The dog eats food."); } public void sound() { System.out.println("The dog barks."); } } //Main class public class Wrap { public static void main(String[] args) { Dog mydog = new Dog(); mydog.eat(); mydog.sound(); Cat mycat = new Cat(); mycat.eat(); mycat.sound(); } } |
Ouput:
1 2 3 4 | The dog eats food. The dog barks. The cat eats food. The cat meows. |
- The implements keyword is used to declare that a class will implement one or more interfaces.
- An interface is a set of abstract methods that classes implementing that interface must implement. Interfaces allow you to define methods that subclasses must implement without specifying the specific implementation.
Interface inheritance
Inheritance is inheriting the properties of the parent class into the child class.
- Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object.
- The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class.
- You can also add new methods and fields in your current class.
- Inheritance represents the IS-A relationship which is also known as the parent-child relationship.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // Animal is a Parent class class Animal { public void eat() { System.out.println("Animal is eating"); } } // Here Dog is derived from Animal class class Dog extends Animal { public static void main(String args[]) { // creating object of Dog class Dog d = new Dog(); // Now, Dog can access eat() method of Animal class d.eat(); } } |
Output:
1 | Animal is eating |
Syntax:
class <Subclass-name> extends <Superclass-name>
{
//methods and fields
}
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | // Java Program to demonstrate // Java Inheritance // Parent Class class Person1 { // Variables int id; String name; // Java Methods void set_Person(int id, String name) { try { this.id = id; this.name = name; } catch (Exception ex) { ex.printStackTrace(); } } void disp_Person() { System.out.print(id + "\t" + name + "\t"); } } // Child Class class Employee1 extends Person1 { int sal; String desgn; void set_Emp(int id, String name, String desgn, int sal) { try { set_Person(id, name); this.desgn = desgn; this.sal = sal; } catch (Exception ex) { ex.printStackTrace(); } } void disp_Emp() { disp_Person(); System.out.print(desgn + "\t" + sal); } // Main function public static void main(String args[]) { Employee1 e1 = new Employee1(); e1.set_Emp(1001, "Manjeet", "AP", 20000); e1.disp_Emp(); } } |
Output:
1 | 1001 Manjeet AP 20000 |