10 Programs
10 Programs
Simple Inheritance
Question: Create a class Animal with a method makeSound() and a subclass Dog that
overrides this method to print "Bark".
java
Copy code
class Animal {
void makeSound() {
System.out.println("Animal sound");
}
}
java
Copy code
class Person {
String name;
Person(String name) {
this.name = name;
}
}
void display() {
System.out.println("Name: " + name + ", Salary: " + salary);
}
java
Copy code
class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}
java
Copy code
class Vehicle {
void display() {
System.out.println("This is a Vehicle");
}
}
java
Copy code
class Student {
String name;
int age;
Student() {
this.name = "Unknown";
this.age = 0;
}
Student(String name) {
this.name = name;
this.age = 0;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
java
Copy code
class Animal {
void speak() {
System.out.println("Animal speaks");
}
}
java
Copy code
abstract class Appliance {
abstract void turnOn();
}
java
Copy code
class Account {
private double balance = 0;
java
Copy code
class Book {
String title;
Book(String title) {
this.title = title;
}
void showTitle() {
System.out.println("Book: " + title);
}
}
class Library {
Book book;
Library(Book book) {
this.book = book;
}
void showBook() {
book.showTitle();
}