Lab2 Shubham
Lab2 Shubham
1. Create a superclass Person with attributes name and age, and a method display().
Create a subclass Student that adds an attribute studentID. Write a program to
create a Student object and display all its attributes.
2. Create a superclass Calculator with a method add(int a, int b). Create a subclass
AdvancedCalculator that overloads the add method to handle three integers.
Program:-
package lab2;
package lab2;
class Calculator {
public int add(int a, int b) {
return a + b;
}
}
3. Create a superclass Vehicle with a method move(). Create subclasses Car and
Bike and call the move() method on each.
Program:-
package lab2;
class Vehicle {
public void move() {
System.out.println("The vehicle is moving");
}
}
Ouput:-
Program:-
Ouput:-
Program:-
package lab2;
class Document {
public void open() {
System.out.println("Opening a generic document");
}
}
wordDoc.open();
pdfDoc.open();
spreadsheetDoc.open();
}
}
Ouput:-
6.Create a class Calculator with overloaded methods add() that take different
numbers and types of parameters: int add(int a, int b), double add(double a, double
b), int add(int a, int b, int c) Write a main class to demonstrate the usage
of these methods
Program:-
package lab2;
class Calculator2 {
public int add(int a, int b) {
return a + b;
}
System.out.println(calc.add(32, 75));
System.out.println(calc.add(117.5, 20));
System.out.println(calc.add(130, 220, 3120));
}
}
Ouput:-