0% found this document useful (0 votes)
21 views11 pages

Lab Assignment3

The document outlines an assignment to implement inheritance in Java by creating classes for car, truck, and motorcycle that extend a vehicle class. It explains the concept of inheritance, its terminology, and provides a sample code demonstrating the implementation. Additionally, it includes questions related to inheritance in Java for further understanding.

Uploaded by

bb7417215
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)
21 views11 pages

Lab Assignment3

The document outlines an assignment to implement inheritance in Java by creating classes for car, truck, and motorcycle that extend a vehicle class. It explains the concept of inheritance, its terminology, and provides a sample code demonstrating the implementation. Additionally, it includes questions related to inheritance in Java for further understanding.

Uploaded by

bb7417215
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/ 11

NAME: Prem Urkude

ROLL NO.:2223970
BATCH : C
Sub :- Jpl - lab

Assignment No. 03
Assignment Title: Implement a concept of inheritance in Java.
Aim: Write a java program to create class car, truck and motorcycle which extends the vehicle
class (attribute registration number, color, type of vehicle) with their own attribute like make, CC
and fuel type. Input data from the user and print all the details.
Pre-Requisites: C/C++
Objective: Demonstrate and apply concept of inheritance.
Outcomes: Students are able to Understand and Apply inheritance concepts with java
programming
Theory:
Inheritance in Java
Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the mechanism in
java by which one class is allowed to inherit the features(fields and methods) of another class. In
Java, inheritance means creating new classes based on existing ones. A class that inherits from
another class can reuse the methods and fields of that class. In addition, you can add new fields
and methods to your current class as well.
Important terminologies used in Inheritance:
● Class: Class is a set of objects which shares common characteristics/ behavior and common
properties/ attributes. Class is not a real-world entity. It is just a template or blueprint or
prototype from which objects are created.
● Super Class/Parent Class: The class whose features are inherited is known as a
superclass(or a base class or a parent class).
● Sub Class/Child Class: The class that inherits the other class is known as a subclass(or a
derived class, extended class, or child class). The subclass can add its own fields and
methods in addition to the superclass fields and methods.
● Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a
new class and there is already a class that includes some of the code that we want, we can
derive our new class from the existing class. By doing this, we are reusing the fields and
methods of the existing class.
Types of Inheritance:-
Example:-
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}

Algorithm/Steps:
● Define superclass vehicle
● Suppose that a program has to deal with motor vehicles, including cars, trucks, and
motorcycles.
● The program could use a class named Vehicle to represent all types of vehicles.
● Since cars, trucks, and motorcycles are types of vehicles, they would be represented by
subclasses of the Vehicle class, as shown in this class hierarchy diagram:
● The Vehicle class would include instance variables such as registration
Number and owner and instance methods such as transferOwnership().
● These are variables and methods common to all vehicles.
● The three subclasses of Vehicle—Car, Truck, and Motorcycle—could then be used to
hold variables and methods specific to particular types of vehicles

Conclusion: Hence we studied and implemented concept of inheritance with java programming.

Code :-
import java.util.*;

class Vehicle {

String reg_no, color, vehicle_type;

// Constructor added to take parameters


Vehicle(String regs_no, String vehcolor, String vehtype) {

reg_no = regs_no;

color = vehcolor;

vehicle_type = vehtype;
}

public void putdata() {


System.out.println("Registration Number:" + reg_no);
System.out.println("Vehicle Color:" + color);
System.out.println("Vehicle Type:" + vehicle_type);
}
}

class Car extends Vehicle {

String make;

int cc;

String fuel_type;

public Car(String regs_no, String vehcolor, String make, int cc, String fuel_type) {
super(regs_no, vehcolor,

"Car");

this.make = make;

this.cc = cc;

this.fuel_type = fuel_type;
}

public void print() {


super.putdata();
System.out.println("Make: " + make);
System.out.println("CC: " + cc);
System.out.println("Fuel type: " + fuel_type);
}
}

class Truck extends Vehicle {

String make;

int cc;

String fuel_type;

public Truck(String regs_no, String vehcolor, String make, int cc, String fuel_type) {
super(regs_no, vehcolor,

"Truck");

this.make = make;

this.cc = cc;

this.fuel_type = fuel_type;
}

public void print() {


super.putdata();
System.out.println("Make: " + make);
System.out.println("CC: " + cc);
System.out.println("Fuel type: " + fuel_type);
}

class Motorcycle extends Vehicle {

String make;

int cc;

String fuel_type;

public Motorcycle(String regs_no, String vehcolor, String make, int cc, String fuel_type) {
super(regs_no, vehcolor,

"Motor Cycle");

this.make = make;

this.cc = cc;

this.fuel_type = fuel_type;
}

public void print() {


super.putdata();
System.out.println("Make: " + make);
System.out.println("CC: " + cc);
System.out.println("Fuel type: " + fuel_type);

class Main {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.println("Enter Car Details:");
System.out.print("Registration Number: ");
String carRegNumber = input.nextLine();
System.out.print("Color: ");
String carColor = input.nextLine();
System.out.print("Make: ");
String carMake = input.nextLine();
System.out.print("CC: ");
int carCC = input.nextInt();
input.nextLine(); // Consume the newline character
System.out.print("Fuel Type: ");
String carType = input.nextLine();

Car car = new Car(carRegNumber, carColor, carMake, carCC, carType);


car.print();

System.out.println("Enter Truck Details:");


System.out.print("Registration Number: ");
String truckRegNumber = input.nextLine();
System.out.print("Color: ");
String truckColor = input.nextLine();
System.out.print("Make: ");
String truckMake = input.nextLine();
System.out.print("CC: ");
int truckCC = input.nextInt();
input.nextLine(); // Consume the newline character
System.out.print("Fuel Type: ");
String truckType = input.nextLine();

Truck truck = new Truck(truckRegNumber, truckColor, truckMake, truckCC,


truckType);
truck.print();

System.out.println("Enter Motorcycle Details:");


System.out.print("Registration Number: ");
String mcRegNumber = input.nextLine();
System.out.print("Color: ");
String mcColor = input.nextLine();
System.out.print("Make: ");
String mcMake = input.nextLine();
System.out.print("CC: ");
int mcCC = input.nextInt();

input.nextLine(); // Consume the newline character


System.out.print("Fuel Type: ");
String mcType = input.nextLine();

Truck mc = new Truck(truckRegNumber, truckColor, truckMake, truckCC, truckType);


mc.print();
}
}

OUTPUT :-
Questions :
1) Why is inheritance important?
2) What does java’s method hiding means?
3) What are different types of inheritance supported by java?
4) What is the difference between inheritance and encapsulation?
5) Explain generalized and specialized classes in java with example.

You might also like