AI May 15, 2023 5 min read

Module 5: Inheritance & Polymorphism

BR

Bhargava Ram Pantina

Web Developer & AI Enthusiast

🔗 Inheritance

Inheritance is one of the key features of OOP that allows one class to inherit properties and methods from another.

                    class Animal {
                        void makeSound() {
                            System.out.println("Some sound");
                        }
                    }
                    
                    class Dog extends Animal {
                        void bark() {
                            System.out.println("Woof");
                        }
                    }
                    
                    public class Main {
                        public static void main(String[] args) {
                            Dog myDog = new Dog();
                            myDog.makeSound(); // inherited method
                            myDog.bark();      // own method
                        }
                    }
                    

🔄 Method Overriding

Subclasses can provide specific implementations of methods defined in the superclass.

                    class Animal {
                        void makeSound() {
                            System.out.println("Some animal sound");
                        }
                    }
                    
                    class Cat extends Animal {
                        @Override
                        void makeSound() {
                            System.out.println("Meow");
                        }
                    }
                    
                    public class Main {
                        public static void main(String[] args) {
                            Animal a = new Cat();
                            a.makeSound(); // Outputs: Meow
                        }
                    }
                    

👥 Polymorphism

Polymorphism allows one interface to be used for a general class of actions. The most common use is when a parent class reference is used to refer to a child class object.

                    class Shape {
                        void draw() {
                            System.out.println("Drawing a shape");
                        }
                    }
                    
                    class Circle extends Shape {
                        void draw() {
                            System.out.println("Drawing a circle");
                        }
                    }
                    
                    class Square extends Shape {
                        void draw() {
                            System.out.println("Drawing a square");
                        }
                    }
                    
                    public class Main {
                        public static void main(String[] args) {
                            Shape s;
                    
                            s = new Circle();
                            s.draw();  // Outputs: Drawing a circle
                    
                            s = new Square();
                            s.draw();  // Outputs: Drawing a square
                        }
                    }
                    
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.