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