0% found this document useful (0 votes)
43 views5 pages

Group - 5 JAVA ASSIGNMENT

The document discusses class inheritance in object-oriented programming. It defines inheritance as a concept where a child class inherits the attributes and methods of a parent class. The document then provides a code example to demonstrate inheritance, with a Vehicle parent class and Car and Motorcycle child classes that inherit properties and methods from Vehicle, overriding the displayInfo() method to add additional class-specific output.

Uploaded by

Papi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views5 pages

Group - 5 JAVA ASSIGNMENT

The document discusses class inheritance in object-oriented programming. It defines inheritance as a concept where a child class inherits the attributes and methods of a parent class. The document then provides a code example to demonstrate inheritance, with a Vehicle parent class and Car and Motorcycle child classes that inherit properties and methods from Vehicle, overriding the displayInfo() method to add additional class-specific output.

Uploaded by

Papi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CLASS INHERITANCE

Inheritance is the concept in OOPs in which one class inherits the attributes and methods of
another class. The class whose properties and methods are inherited is known as the Parent class.
And the class that inherits the properties from the parent class is the Child class. It is a
fundamental concept in OOP that promotes code reuse and establishes relationships between
classes.

PROGRAM THAT DEMONSTRATES THE CONCEPT OF INHERISTANCE

// Parent class

class Vehicle {

private String brand;

private String color;

public Vehicle(String brand, String color) {

this.brand = brand;

this.color = color;

public void displayInfo() {

System.out.println("Brand: " + brand);

System.out.println("Color: " + color);

// Child class inheriting from Vehicle

class Car extends Vehicle {


private int numDoors;

public Car(String brand, String color, int numDoors) {

super(brand, color);

this.numDoors = numDoors;

public void displayInfo() {

super.displayInfo(); // Call parent class method

System.out.println("Number of doors: " + numDoors);

// Child class inheriting from Vehicle

class Motorcycle extends Vehicle {

private boolean hasSidecar;

public Motorcycle(String brand, String color, boolean hasSidecar) {

super(brand, color);

this.hasSidecar = hasSidecar;

public void displayInfo() {

super.displayInfo(); // Call parent class method

System.out.println("Has sidecar: " + hasSidecar);


}

// Main class

public class InheritanceDemo {

public static void main(String[] args) {

Car myCar = new Car("Toyota", "Red", 4);

myCar.displayInfo(); // Calls Car's displayInfo() method

System.out.println();

Motorcycle myMotorcycle = new Motorcycle("Honda", "Blue", true);

myMotorcycle.displayInfo(); // Calls Motorcycle's displayInfo() method

In this program, we have a parent class called Vehicle with a constructor and a method
displayInfo() that prints the brand and color of the vehicle. The Car and Motorcycle classes
inherit from the Vehicle class using the extends keyword.

The Car class has an additional property numDoors and overrides the displayInfo() method to
include the number of doors in the output.

The Motorcycle class has an additional property hasSidecar and also overrides the displayInfo()
method to include whether or not it has a sidecar.

In the main method, we create objects of Car and Motorcycle and call their respective
displayInfo() methods. The output will demonstrate how the child classes inherit properties and
methods from the parent class.
Question C

public class OverloadExample {

public static void main(String[] args) {

int int1 = 10;

int int2 = 20;

float float1 = 3.5f;

float float2 = 2.8f;

int maxInt = max(int1, int2);

float maxFloat = max(float1, float2);

System.out.println("Maximum integer value: " + maxInt);

System.out.println("Maximum float value: " + maxFloat);

public static int max(int num1, int num2) {

return Math.max(num1, num2);

public static float max(float num1, float num2) {

return Math.max(num1, num2);

}
In this program, we have an OverloadExample class with a main() method. The program declares
four variables: int1 and int2 for integers, and float1 and float2 for floating-point numbers.

The max() method is overloaded, which means we have two methods with the same name but
different parameter types. The first max() method takes two int parameters and uses the
Math.max() method to determine the maximum value between the two integers. The second
max() method takes two float parameters and uses the Math.max() method to determine the
maximum value between the two floating-point numbers.

In the main() method, we call the overloaded max() methods with the given integer and floating-
point values. The returned maximum values are stored in maxInt and maxFloat variables,
respectively.

You might also like