The extends keyword in Java is used to indicate that a class is inheriting from a superclass. This is a fundamental aspect of object-oriented programming in Java, allowing a new class to inherit fields and methods from an existing class.
Usage
The extends keyword is used in class declarations to establish an inheritance relationship between two classes.
Syntax
classSubclassNameextendsSuperclassName{// class body}Didukung oleh
SubclassName: The name of the new class that is inheriting.
SuperclassName: The name of the existing class being inherited from.
Examples
Example 1: Basic Inheritance
classAnimal{voideat(){
System.out.println("This animal eats food.");}}classDogextendsAnimal{voidbark(){
System.out.println("The dog barks.");}}publicclassMain{publicstaticvoidmain(String[] args){
Dog dog =newDog();
dog.eat();// Inherited method
dog.bark();// Method of Dog class}}Didukung oleh
In this example, the Dog class extends the Animal class, inheriting its eat method. The Dog class also has its own method bark. When an instance of Dog is created, both the inherited eat method and the bark method can be called.
Example 2: Overriding Methods
classAnimal{voidsound(){
System.out.println("This animal makes a sound.");}}classCatextendsAnimal{
@Override
voidsound(){
System.out.println("The cat meows.");}}publicclassMain{publicstaticvoidmain(String[] args){
Cat cat =newCat();
cat.sound();// Overridden method}}Didukung oleh
Here, the Cat class extends the Animal class and overrides the sound method to provide its own implementation. The @Override annotation is used to indicate that the method is being overridden.
Example 3: Inheriting Fields
classPerson{
String name;}classEmployeeextendsPerson{
int employeeId;}publicclassMain{publicstaticvoidmain(String[] args){
Employee emp =newEmployee();
emp.name ="John Doe";// Inherited field
emp.employeeId =12345;
System.out.println("Name: "+ emp.name +", Employee ID: "+ emp.employeeId);}}Didukung oleh
In this example, the Employee class extends the Person class and inherits its name field. The Employee class also has its own field employeeId.
Tips and Best Practices
Use Inheritance Judiciously: Inheritance should be used when there is a clear "is-a" relationship between the subclass and the superclass.
Override Methods Properly: Use the @Override annotation when overriding methods to ensure that you are correctly overriding a method from the superclass.
Avoid Deep Inheritance Hierarchies: Deep inheritance hierarchies can make the code difficult to understand and maintain. Prefer composition over inheritance when appropriate.
Access Superclass Methods: Use super to call superclass methods from the subclass if needed.
classAnimal{voideat(){
System.out.println("This animal eats food.");}}classDogextendsAnimal{voideat(){super.eat();// Call superclass method
System.out.println("The dog eats dog food.");}}Didukung oleh
Constructor Chaining: Use super() to call the constructor of the superclass from the subclass constructor.
classPerson{Person(String name){
System.out.println("Person's name is "+ name);}}classEmployeeextendsPerson{Employee(String name, int employeeId){super(name);// Call superclass constructor
System.out.println("Employee ID is "+ employeeId);}}Didukung oleh
Understanding Inheritance
Inheritance is a key concept in object-oriented programming (OOP) that establishes a relationship between a more general superclass and a more specific subclass. This relationship allows the subclass to inherit attributes and methods from the superclass, promoting code reusability and logical structuring.
Is-A Relationship: The extends keyword signifies an "is-a" relationship. For example, a Dog is an Animal, and an Employee is a Person.
Single Inheritance: Java supports single inheritance, meaning a class can only extend one other class to avoid ambiguity and complexity.
Combining Extends and Implements: A class can extend another class and implement multiple interfaces simultaneously. This combination allows a class to inherit from a superclass and adhere to multiple contracts defined by interfaces.
classAnimal{voidmove(){
System.out.println("This animal moves.");}}interfacePet{voidplay();}classDogextendsAnimalimplementsPet{voidbark(){
System.out.println("The dog barks.");}publicvoidplay(){
System.out.println("The dog plays.");}}publicclassMain{publicstaticvoidmain(String[] args){
Dog dog =newDog();
dog.move();// Inherited method from Animal
dog.bark();// Method of Dog class
dog.play();// Implemented method from Pet interface}}Didukung oleh
In this example, the Dog class extends the Animal class and implements the Pet interface, showcasing the combination of inheritance and interface implementation.
The
extendskeyword in Java is used to indicate that a class is inheriting from a superclass. This is a fundamental aspect of object-oriented programming in Java, allowing a new class to inherit fields and methods from an existing class.Usage
The
extendskeyword is used in class declarations to establish an inheritance relationship between two classes.Syntax
SubclassName: The name of the new class that is inheriting.SuperclassName: The name of the existing class being inherited from.Examples
Example 1: Basic Inheritance
In this example, the
Dogclass extends theAnimalclass, inheriting itseatmethod. TheDogclass also has its own methodbark. When an instance ofDogis created, both the inheritedeatmethod and thebarkmethod can be called.Example 2: Overriding Methods
Here, the
Catclass extends theAnimalclass and overrides thesoundmethod to provide its own implementation. The@Overrideannotation is used to indicate that the method is being overridden.Example 3: Inheriting Fields
In this example, the
Employeeclass extends thePersonclass and inherits itsnamefield. TheEmployeeclass also has its own fieldemployeeId.Tips and Best Practices
@Overrideannotation when overriding methods to ensure that you are correctly overriding a method from the superclass.superto call superclass methods from the subclass if needed.super()to call the constructor of the superclass from the subclass constructor.Understanding Inheritance
Inheritance is a key concept in object-oriented programming (OOP) that establishes a relationship between a more general superclass and a more specific subclass. This relationship allows the subclass to inherit attributes and methods from the superclass, promoting code reusability and logical structuring.
extendskeyword signifies an "is-a" relationship. For example, aDogis anAnimal, and anEmployeeis aPerson.In this example, the
Dogclass extends theAnimalclass and implements thePetinterface, showcasing the combination of inheritance and interface implementation.