extends
keyword for class inheritance.
implements
keyword for interface inheritance.
class Vehicle
{
void start()
{
System.out.println("Vehicle starts.");
}
}
class Car extends Vehicle
{
void drive()
{
System.out.println("Car drives.");
}
}
public class MainApp
{
public static void main(String[] args)
{
Car myCar = new Car();
myCar.start(); // inherited from Vehicle
myCar.drive(); // specific to Car
}
}
interface Animal
{
void eat();
}
class Dog implements Animal
{
public void eat()
{
System.out.println("Dog eats.");
}
}
public class MainApp
{
public static void main(String[] args)
{
Dog myDog = new Dog();
myDog.eat(); // inherited from Animal
//Animal myAnimal = new Animal(); // error because we cannot create an object of an interface
}
}
super
keyword is used to refer to the parent class, such as accessing parent class methods or constructors.
this
keyword is used to refer to the current class instance, commonly used to differentiate between instance variables and parameters.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.