Parent and Child Classes
π Scenario: You are creating a simple program to represent vehicles. You want to show how a child class can inherit properties from a parent class.
π― Goal: Build a parent class called Vehicle and a child class called Car that inherits from Vehicle. Then create an object of Car and print its details.
π What You'll Learn
Create a parent class named
Vehicle with a String field called brand.Create a child class named
Car that extends Vehicle and adds an int field called year.Create a constructor in
Vehicle to set the brand.Create a constructor in
Car to set both brand and year.Create a method
displayInfo() in Car that prints the brand and year.Create a
Car object with brand "Toyota" and year 2020, then call displayInfo().π‘ Why This Matters
π Real World
Understanding parent and child classes helps organize code by sharing common features and adding specific details, like different types of vehicles.
πΌ Career
Inheritance is a key concept in object-oriented programming used in many software development jobs to build reusable and maintainable code.
Progress0 / 4 steps