Write a Java program to Demonstrate the protected access specifier


This Java program demonstrates method overriding, a concept in object-oriented programming where a subclass provides a specific implementation of a method that is already defined in its superclass. In this program, there are two classes: Vehicle (the base class) and Car (the derived class), and the display method in the Car class overrides the display method in the Vehicle class. Here's an explanation of the program:

  • public class OverrideMethod: This is the main class that contains the main method, the entry point of the program.
  • public static void main(String[] args): The main method is the starting point of the program. It creates an object of the Car class and demonstrates method overriding by calling the display method on this object.
  • Car car = new Car();: This line creates an object of the Car class.
  • car.display();: This line calls the display method on the car object. Because the Car class has overridden the display method, the specific implementation in the Car class is executed. Therefore, the output will be "This is a Car" instead of "This is a Vehicle" that is defined in the Vehicle class.
  • class Vehicle: This is the base class that represents generic vehicle properties and behaviors.
  • void display(): The display method of the Vehicle class simply prints a message indicating that it is a vehicle.
  • class Car extends Vehicle: This is the derived class (subclass) that inherits from the Vehicle class. It represents a specific type of vehicle, a car.
  • @Override void display(): This line indicates that the display method in the Car class is intended to override the display method in the Vehicle class. The @Override annotation is not mandatory but is good practice to indicate that you are intentionally overriding a method from the superclass.
  • The overridden display method in the Car class provides a specific implementation, which is different from the implementation in the Vehicle class. It prints a message indicating that it is a car, and this specific implementation is executed when the display method is called on an object of the Car class.

This program demonstrates method overriding, where a method in the derived class (Car) provides its own implementation of a method that is already defined in the base class (Vehicle). When the overridden method is called on an object of the derived class, the specific implementation in the derived class is executed.


Source Code

public class AccessSpecifier	// Main class
{
	public static void main(String[] args)
	{        
		Car car = new Car();   // Create an object of the child class        
		car.setBrand("Toyota");	 // Set the brand using the public method        
		car.displayBrand();	  // Access the protected member and method
	}
}
class Vehicle	// Parent class
{
	protected String brand;
 
	protected void displayBrand()
	{
		System.out.println("Brand : " + brand);
	}
}
class Car extends Vehicle	// Child class
{
	public void setBrand(String brand)
	{
		this.brand = brand;
	}
}

Output

Brand : Toyota