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

Java Lab Programs

The document contains Java programs demonstrating the concepts of constructors, static members, and inheritance. The first program showcases a Car class with both no-argument and parameterized constructors, while the second program illustrates a Student class with a static member and method to change the college name. The third program features an Animal class and a Dog subclass, highlighting method inheritance and functionality.

Uploaded by

dd4692074
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)
2 views5 pages

Java Lab Programs

The document contains Java programs demonstrating the concepts of constructors, static members, and inheritance. The first program showcases a Car class with both no-argument and parameterized constructors, while the second program illustrates a Student class with a static member and method to change the college name. The third program features an Animal class and a Dog subclass, highlighting method inheritance and functionality.

Uploaded by

dd4692074
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

Ex.

No: 1A Constructor

Program:

public class Car {

String brand;

int year;

public Car() {

brand = "Unknown";

year = 2000;

[Link]("No-Argument Constructor called.");

public Car(String brandName, int manufacturingYear) {

brand = brandName;

year = manufacturingYear;

[Link]("Parameterized Constructor called.");

public void displayDetails() {

[Link]("Car Brand: " + brand + ", Year: " + year);

public static void main(String[] args) {

[Link]("--- Creating first car ---");

Car car1 = new Car();

[Link]();

[Link]("\n--- Creating second car ---");

Car car2 = new Car("Toyota", 2024);

[Link](); }}
OUTPUT:

No-Argument Constructor Called

Student Name: Unknown

Student ID: 0

---------------------------

Parameterized Constructor Called

Student Name: Sweta

Student ID: 68
Ex. No: 1b Static Member

class Student {
int rollNo;
String name;

static String collegeName = "CAHCET";

Student(int r, String n) {
rollNo = r;
name = n;
}

static void changeCollege(String newCollege) {


collegeName = newCollege;
}

void display() {
[Link](rollNo + " " + name + " " + collegeName);
}
}

public class Main {


public static void main(String[] args) {
// Create student objects
Student s1 = new Student(101, "Alice");
Student s2 = new Student(102, "Bob");

[Link]("--- Initial Data ---");


[Link]();
[Link]();

[Link]("ANNA University");

[Link]("\n--- Data After Static Method Change ---");


[Link]();
[Link]();
}
}

OUTPUT:

--- Initial Data ---

101 Alice CAHCET


102 Bob CAHCET

--- Data After Static Method Change ---

101 Alice ANNA University

102 Bob ANNA University


Ex. No: 2 Inheritance

Program:

class Animal {

void eat() {

[Link]("This animal eats food.");

class Dog extends Animal {

void bark() {

[Link]("The dog barks.");

public class Main {

public static void main(String[] args) {

Dog myDog = new Dog();

[Link]();

[Link]();

OUTPUT:

This animal eats food.

The dog barks.

You might also like