What is Java How To Extend A Class?
Java is an object-oriented programming language used for building cross-platform applications. To extend a class in Java, you use the extends
keyword in the class declaration. This allows the new class (subclass) to inherit properties and methods from the existing class (superclass). Here's an example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
In this example, Dog
extends Animal
and can override its methods.