LAB-7
Program1: Write a program in constructor using final
keyword and finalize keyword.
class MyClass {
private final String name;
private int age;
MyClass(final String name, int age) {
[Link] = name;
[Link] = age;
}
void display() {
[Link]("Name: " + [Link] + ", Age: " +
[Link]);
}
@Override
protected void finalize() throws Throwable {
try {
[Link]("finalize() called for object: " +
[Link]);
} finally {
[Link]();
}
}
}
public class FinalKeywordFinalizeExample {
public static void main(String[] args) {
MyClass obj1 = new MyClass("A", 25);
MyClass obj2 = new MyClass("B", 30);
[Link]();
[Link]();
obj1 = null;
obj2 = null;
[Link]();
try {
[Link](2000);
} catch (InterruptedException e) {
[Link]();
}
[Link]("End of Program");
}
}
Program2: Write a program on single inheritance.
class Vehicle {
String brand;
int speed;
public Vehicle(String brand, int speed) {
[Link] = brand;
[Link] = speed;
}
public void displayDetails() {
[Link]("Brand: " + brand);
[Link]("Speed: " + speed + " km/h");
}
}
class Car extends Vehicle {
int doors;
public Car(String brand, int speed, int doors) {
super(brand, speed);
[Link] = doors;
}
public void displayCarDetails() {
displayDetails();
[Link]("Doors: " + doors);
}
}
public class SingleInheritanceExample {
public static void main(String[] args) {
Car myCar = new Car("Toyota", 180, 4);
[Link]();
}
}
Program3: Write a program on multilevel inheritance,
hierarchical inheritance
// Grandparent class (Superclass of Parent class)
class Grandparent {
String name;
public Grandparent(String name) {
[Link] = name;
}
public void displayGrandparentInfo() {
[Link]("Grandparent's Name: " + name);
}
}
// Parent class that inherits from Grandparent (Multilevel
inheritance)
class Parent extends Grandparent {
String occupation;
public Parent(String name, String occupation) {
// Calling the constructor of the Grandparent class
using super
super(name);
[Link] = occupation;
}
public void displayParentInfo() {
displayGrandparentInfo(); // Calling Grandparent's
method
[Link]("Parent's Occupation: " +
occupation);
}
}
// Child class that inherits from Parent (Multilevel
inheritance)
class Child extends Parent {
int age;
public Child(String name, String occupation, int age) {
// Calling the constructor of the Parent class using
super
super(name, occupation);
[Link] = age;
}
public void displayChildInfo() {
displayParentInfo(); // Calling Parent's method
[Link]("Child's Age: " + age);
}
}
// Parent class (for hierarchical inheritance)
class ParentForChild1And2 {
String familyName;
public ParentForChild1And2(String familyName) {
[Link] = familyName;
}
public void displayFamilyName() {
[Link]("Family Name: " + familyName);
}
}
// Child1 class that inherits from ParentForChild1And2
(Hierarchical inheritance)
class Child1 extends ParentForChild1And2 {
String hobby;
public Child1(String familyName, String hobby) {
// Calling the constructor of ParentForChild1And2
class
super(familyName);
[Link] = hobby;
}
public void displayChild1Info() {
displayFamilyName(); // Calling Parent's method
[Link]("Child1's Hobby: " + hobby);
}
}
// Child2 class that inherits from ParentForChild1And2
(Hierarchical inheritance)
class Child2 extends ParentForChild1And2 {
int grade;
public Child2(String familyName, int grade) {
// Calling the constructor of ParentForChild1And2
class
super(familyName);
[Link] = grade;
}
public void displayChild2Info() {
displayFamilyName(); // Calling Parent's method
[Link]("Child2's Grade: " + grade);
}
}
public class InheritanceExample {
public static void main(String[] args) {
// Multilevel Inheritance Example
Child child = new Child("John", "Engineer", 25);
[Link]();
[Link]();
// Hierarchical Inheritance Example
Child1 child1 = new Child1("Smith", "Reading");
child1.displayChild1Info();
[Link]();
Child2 child2 = new Child2("Smith", 10);
child2.displayChild2Info();
}
}
Lab-8
Program1: Write a program on method overloading.
class Calculator {
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
public double add(double a, double b) {
return a + b;
}
public double add(int a, double b) {
return a + b;
}
public double add(double a, int b) {
return a + b;
}
}
public class MethodOverloadingExample {
public static void main(String[] args) {
Calculator calc = new Calculator();
[Link]("Sum of two integers: " +
[Link](10, 20));
[Link]("Sum of three integers: " +
[Link](10, 20, 30));
[Link]("Sum of two doubles: " +
[Link](10.5, 20.5));
[Link]("Sum of an integer and a double: " +
[Link](10, 20.5));
[Link]("Sum of a double and an integer: " +
[Link](10.5, 20));
}
}
Program2: Write a program on final method.
class Vehicle {
public final void startEngine() {
[Link]("Engine started.");
}
}
class Car extends Vehicle {
public void honkHorn() {
[Link]("Car horn honked!");
}
}
public class FinalMethodExample {
public static void main(String[] args) {
Vehicle myVehicle = new Vehicle();
[Link]();
Car myCar = new Car();
[Link]();
[Link]();
}
}
Program3: Write programs on interfaces.
interface Animal {
static void info() {
[Link]("Animals are living beings that
move and eat.");
}
void sound();
}
class Dog implements Animal {
public void sound() {
[Link]("The dog barks");
}
}
public class InterfaceExample4 {
public static void main(String[] args) {
[Link]();
Dog myDog = new Dog();
[Link]();
}
}
LAB-09
Program1: Write a program on packages including .lang,
.net and .until .
import [Link].*;
import [Link].*;
import [Link].*;
public class PackageExample {
public static void main(String[] args) {
String greeting = "Hello, welcome to the Java Packages
tutorial!";
[Link]("Original Greeting: " + greeting);
[Link]("Greeting in uppercase: " +
[Link]());
try {
URL url = new URL("[Link]
[Link]("URL: " + url);
[Link]("Protocol: " + [Link]());
[Link]("Host: " + [Link]());
} catch (MalformedURLException e) {
[Link]("Invalid URL format!");
}
Date currentDate = new Date();
[Link]("Current Date and Time: " +
currentDate);
Scanner scanner = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Hello, " + name + "!");
[Link]();
}
}