Inheritance in Java

 Inheritance in Java


Inheritance is a feature of object-oriented programming that allows you to create new classes based on existing classes. The new classes inherit properties and methods from the existing classes.


Example code:


csharp


public class Animal {

    public void speak() {

        System.out.println("Animal speaks.");

    }

}


public class Dog extends Animal {

    public void speak() {

        System.out.println("Dog barks.");

    }

}


Dog myDog = new Dog();

myDog.speak(); // prints "Dog barks."

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...