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

Lab

Uploaded by

abhikannadiga20
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)
4 views5 pages

Lab

Uploaded by

abhikannadiga20
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/ 5

5.

Write a Java program that demonstrates the creation and accessing of


objects and methods.
class Student
{
String name;
int age;
String course;

public Student(String name, int age, String course)


{
this.name = name;
this.age = age;
this.course = course;
}

public void displayInfo()


{
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Course: " + course);
}

public void enrollCourse(String newCourse)


{
course = newCourse;
System.out.println(name + " enrolled in " + course);
}

public static void main(String[] args)


{
Student student1 = new Student("Chaithra", 20, "Computer Science");
Student student2 = new Student("Harshitha", 22, "Mathematics");
student1.displayInfo();
student1.enrollCourse("Data Science");
System.out.println();
student2.displayInfo();
student2.enrollCourse("Statistics");
}
}

7.Write a Java program to demonstrates the concept of single inheritance:

class Animal
{
void eat()
{
System.out.println("Eating...");
}
void sleep()
{
System.out.println("Sleeping...");
}
}

class Dog extends Animal


{
void bark()
{
System.out.println("Barking...");
}
}
public class singleinheritance
{
public static void main(String[] args)
{
Dog dog = new Dog();
dog.eat();
dog.sleep();
dog.bark();
}
}

8. Write a Java program to demonstrates the concept of Multilevel


inheritance

class Animal
{
void eat()
{
System.out.println("Eating...");
}
void sleep()
{
System.out.println("Sleeping...");
}
}
class Mammal extends Animal
{
void walk()
{
System.out.println("Walking...");
}
}

class Dog extends Mammal


{
void bark()
{
System.out.println("Barking...");
}
}

class MultilevelInheritance
{
public static void main(String[] args)
{
Dog dog = new Dog();
dog.eat();
dog.sleep();
dog.walk();
dog.bark();
}
}

You might also like