- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java and multiple inheritance
Java does not support multiple inheritance. This means that a class cannot extend more than one class. Therefore, following is illegal
public class extends Animal, Mammal{}
However, a class can implement one or more interfaces, which has helped Java get rid of the impossibility of multiple inheritance. The extends keyword is used once, and the parent interfaces are declared in a comma-separated list. For example, if the Hockey interface extended both Sports and Event, it would be declared as −
public interface Hockey extends Sports, Event
Following example demonstrates the running example.
Example
interface Event { public void start(); } interface Sports { public void play(); } interface Hockey extends Sports, Event{ public void show(); } public class Tester{ public static void main(String[] args){ Hockey hockey = new Hockey() { public void start() { System.out.println("Start Event"); } public void play() { System.out.println("Play Sports."); } public void show() { System.out.println("Show Hockey."); } }; hockey.start(); hockey.play(); hockey.show(); } }
Output
Start Event Play Sports. Show Hockey.
- Related Articles
- C# and Multiple Inheritance
- Multiple inheritance by Interface in Java
- Java Program to Implement Multiple Inheritance
- Difference Between Single and Multiple Inheritance
- Why multiple inheritance is not supported by Java?
- Why multiple inheritance is not supported in Java
- How multiple inheritance is implemented using interfaces in Java?
- Multiple Inheritance in C++
- Multiple inheritance in JavaScript
- Does Python support multiple inheritance?
- Does Java support multiple inheritance? Why? How can we resolve this?
- What is diamond problem in case of multiple inheritance in java?
- Interfaces and inheritance in Java Programming
- Inheritance in Java
- Difference between inheritance and composition in Java

Advertisements