AI May 15, 2023 5 min read

Module 10: Inheritance

BR

Bhargava Ram Pantina

Web Developer & AI Enthusiast

👪 What is Inheritance?

Inheritance is a mechanism where one class acquires the properties and behaviors (fields and methods) of another class. It supports reusability and method overriding.

In Java, inheritance is implemented using the extends keyword.

🧬 Single Inheritance Example

                    class Animal {
                        void eat() {
                            System.out.println("This animal eats food");
                        }
                    }
                    
                    class Dog extends Animal {
                        void bark() {
                            System.out.println("Dog barks");
                        }
                    
                        public static void main(String[] args) {
                            Dog d = new Dog();
                            d.eat();
                            d.bark();
                        }
                    }
                    

🔁 Method Overriding

Method overriding allows a subclass to provide a specific implementation of a method already provided by its superclass.

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

🔗 Types of Inheritance in Java

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance

Note: Java does not support multiple inheritance with classes, but it supports multiple inheritance with interfaces.

Artificial Intelligence Web Development Technology Trends Machine Learning UX Design

Share this article

About the Author

BR

Bhargava Ram Pantina

Bhargava Ram is a web developer and AI enthusiast with over 8 years of experience in the tech industry. He specializes in creating innovative web solutions that leverage the latest technologies to solve real-world problems.

Related Articles

AI Mar 15, 2023

Implementing ChatGPT in Your Web Application: A Step-by-Step Tutorial

Learn how to integrate OpenAI's ChatGPT into your web application to create intelligent chatbots and enhance user interactions.

Read More →
Web Development Apr 22, 2023

Building Accessible Web Forms: A Complete Guide

Learn how to create web forms that are accessible to all users, including those with disabilities. This comprehensive guide covers ARIA attributes, keyboard navigation, and more.

Read More →

Subscribe to My Newsletter

Get the latest articles and resources delivered straight to your inbox.

No spam, just valuable content. Unsubscribe anytime.