Java EE 8 Lab Manual - Unit 1
1. Interface Implementation
interface Vehicle {
void start();
}
class Car implements Vehicle {
public void start() {
System.out.println("Car started...");
}
}
public class VehicleDemo {
public static void main(String[] args) {
Vehicle v = new Car();
v.start();
}
}
Sample Output: (Attach screenshot here manually if needed)
2. Single Inheritance Example
class Person {
void display() {
System.out.println("This is a person.");
}
}
class Student extends Person {
void show() {
System.out.println("This is a student.");
}
}
public class InheritanceDemo {
public static void main(String[] args) {
Student s = new Student();
s.display();
s.show();
}
}
Sample Output: (Attach screenshot here manually if needed)
3. Abstract Class and Polymorphism
abstract class Shape {
abstract double area();
}
class Circle extends Shape {
double radius = 5;
double area() {
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape {
double length = 4, width = 3;
double area() {
return length * width;
}
}
public class ShapeDemo {
public static void main(String[] args) {
Shape s1 = new Circle();
Shape s2 = new Rectangle();
System.out.println("Circle Area: " + s1.area());
System.out.println("Rectangle Area: " + s2.area());
}
}
Sample Output: (Attach screenshot here manually if needed)
4. Method Overloading and Overriding
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
class AdvancedCalculator extends Calculator {
@Override
int add(int a, int b) {
return super.add(a, b) + 10;
}
}
public class PolymorphismDemo {
public static void main(String[] args) {
Calculator calc = new AdvancedCalculator();
System.out.println("2-arg Add: " + calc.add(2, 3));
System.out.println("3-arg Add: " + calc.add(2, 3, 4));
}
}
Sample Output: (Attach screenshot here manually if needed)
5. Using Scanner for Input
import java.util.Scanner;
public class UserInputDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Hello " + name + ", your age is " + age +
".");
}
}
Sample Output: (Attach screenshot here manually if needed)