Internship training
Task:
Java Programs using Inheritance
1. Single Inheritance:
Programs:
Use single inheritance:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
d.bark();
}
}
Output:
Animal make a sound
Dog barks
Screen Short:
Result:
Thus the program has been successfully executed and
verified.
Using Scanner:
import java.util.Scanner;
class Animal {
void sound(String animalName) {
System.out.println(animalName + " makes a sound.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks loudly.");
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter animal name: ");
String name = sc.nextLine();
Dog d = new Dog();
d.sound(name);
d.bark();
}}
Output:
Enter animal name: Dog
Dog makes a sound
Dog barks loudly
Screen Short:
Result:
Thus the program has been successfully executed and
verified.
Using Constructor:
import java.util.Scanner;
class Person {
Person() {
System.out.println("Constructor of Person class.");
}
}
class Student extends Person {
Student() {
super(); // call to parent constructor
System.out.println("Constructor of Student class.");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Creating Student object...");
Student s = new Student();
}}
Output:
Creating Student object...
Constructor of Person class.
Constructor of Student class.
Screen Short:
Result:
Thus the program has been successfully executed and
verified.
Access Method:
import java.util.Scanner;
class Person {
void greet() {
System.out.println("Hello from the Person class!");
}
}
class Student extends Person {
void study() {
System.out.println("Student is studying.");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Student s = new Student();
s.greet();
s.study();
}
}
Output:
Single Inheritance
Access the Method
Screen Short:
Result:
Thus the program has been successfully executed and
verified.
Access Variable:
class Person {
String name = "John Doe"; // variable in parent class
}
class Student extends Person {
void show() {
System.out.println("Name from Person class: " +
name);
Public class Main{
public static void main(String[] args) {
Student s = new Student();
s.show(); // accessing parent class variable
Output:
Name: Sandhiya
Screen Short:
Result:
Thus the program has been successfully executed and
verified.
2. Multilevel Inheritance:
Program:
Simple program:
class Person {
String name = "Sandhiya";
}
class Student extends Person {
String roll = "79";
}
class CollegeStudent extends Student {
String college = "K M G College";
void showDetails() {
System.out.println("Name: " + name);
System.out.println("Roll: " + roll);
System.out.println("College: " + college);
}
}
public class Main {
public static void main(String[] args) {
CollegeStudent cs = new CollegeStudent();
cs.showDetails();
}
}
Output:
Name: Sandhiya
Roll: 79
College: K M G college
Screen Short:
Result:
Thus the program has been successfully executed and
verified.
Using Scanner:
import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("Hello, " + name + "! You are " +
age + " years old.");
scanner.close();
}
}
Output:
Enter your name: Sandhiya
Enter your age: 25
Hello, Sandhiya! You are 25 years old.
Screen Short:
Result:
Thus the program has been successfully executed and
verified.
Using Constructor:
class Person {
Person() {
System.out.println("Person constructor called");
}
}
class Student extends Person {
Student() {
super(); // Calls Person constructor
System.out.println("Student constructor called");
}
}
class CollegeStudent extends Student {
CollegeStudent() {
super(); // Calls Student constructor
System.out.println("CollegeStudent constructor
called");
}
}
public class MultilevelConstructorExample {
public static void main(String[] args) {
CollegeStudent cs = new CollegeStudent();
}
}
Output:
Person constructor called
Student constructor called
CollegeStudent constructor called
Screen Short:
Result:
Thus the program has been successfully executed and
verified.
Access method:
class Person {
void personInfo() {
System.out.println("This is a Person.");
}
}
class Student extends Person {
void studentInfo() {
System.out.println("This is a Student.");
}
}
class CollegeStudent extends Student {
void collegeStudentInfo() {
System.out.println("This is a College Student.");
}
}
public class Main {
public static void main(String[] args) {
CollegeStudent cs = new CollegeStudent();
cs.personInfo();
cs.studentInfo();
cs.collegeStudentInfo();
}
}
Output:
This is a Person.
This is a Student.
This is a College Student.
Screen Short:
Result:
Thus the program has been successfully executed and
verified.
Access Variable:
class Person {
int id = 1;
}
class Student extends Person {
int age = 20;
}
class CollegeStudent extends Student {
int marks = 85;
void displayDetails() {
System.out.println("ID: " + id);
System.out.println("Age: " + age);
System.out.println("Marks: " + marks);
}
}
public class Main {
public static void main(String[] args) {
CollegeStudent cs = new CollegeStudent();
cs.displayDetails();
}
}
Output:
ID: 1
Age: 20
Marks: 85
Screen Short:
Result:
Thus the program has been successfully executed and
verified.
3. Hierarchical Inheritance:
Program:
Simple program:
class Animal {
void eat() {
System.out.println("Animal eats food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
Cat c = new Cat();
c.eat();
c.meow();
}
}
Output:
Animal eats food
Dog barks
Animal eats food
Cat meows
Screen Short:
Result:
Thus the program has been successfully executed and
verified.
Using constructor:
class Vehicle {
Vehicle() {
System.out.println("Vehicle constructor called");
}
void showType() {
System.out.println("This is a vehicle");
}
}
class Car extends Vehicle {
String model = "Sedan";
Car() {
System.out.println("Car constructor called");
}
void showModel() {
System.out.println("Car model: " + model);
}
}
class Bike extends Vehicle {
String brand = "Yamaha";
Bike() {
System.out.println("Bike constructor called");
}
void showBrand() {
System.out.println("Bike brand: " + brand);
}
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.showType();
c.showModel();
System.out.println();
Bike b = new Bike();
b.showType();
b.showBrand();
}
}
Output:
Vehicle constructor called
Car constructor called
This is a vehicle
Car model: Sedan
Vehicle constructor called
Bike constructor called
This is a vehicle
Bike brand: Yamaha
Screen Short:
Result:
Thus the program has been successfully executed and
verified.
Access class name:
class NumberData {
int number = 100;
void showNumber() {
System.out.println("Base number: " + number);
}
}
class Square extends NumberData {
void showSquare() {
System.out.println("Square: " + (number * number));
}
}
class Cube extends NumberData {
void showCube() {
System.out.println("Cube: " + (number * number *
number));
}
}
public class Main {
public static void main(String[] args) {
Square s = new Square();
s.showNumber();
s.showSquare();
System.out.println();
Cube c = new Cube();
c.showNumber();
c.showCube();
}
}
Output:
Base number: 100
Square: 10000
Base number: 100
Cube: 1000000
Screen Short:
Result:
Thus the program has been successfully executed and
verified.
Using Scanner:
import java.util.Scanner;
class Numbers {
int a, b;
void getNumbers() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
a = sc.nextInt();
System.out.print("Enter second number: ");
b = sc.nextInt();
}
}
class Subtraction extends Numbers {
void showSubtraction() {
int result = a - b;
System.out.println("Subtraction: " + result);
}
}
class Addition extends Numbers {
void showAddition() {
int result = a + b;
System.out.println("Addition: " + result);
}
}
public class Main {
public static void main(String[] args) {
Subtraction sub = new Subtraction();
sub.getNumbers();
sub.showSubtraction();
System.out.println();
Addition add = new Addition();
add.a = sub.a;
add.b = sub.b;
add.showAddition();
}
}
Output:
Enter first number: 20
Enter second number: 15
Subtraction: 5
Addition: 35
Screen Short:
Result:
Thus the program has been successfully executed and
verified.
Access Method:
class Student {
int id = 123;
void showId() {
System.out.println("Student ID: " + id);
}
}
class Course extends Student {
void showCourse() {
System.out.println("Course: B.Com");
}
}
class Department extends Student {
void showDepartment() {
System.out.println("Department: Commerce");
}
}
public class Main {
public static void main(String[] args) {
Course c = new Course();
c.showId();
c.showCourse();
System.out.println();
Department d = new Department();
d.showId();
d.showDepartment();
}
}
Output:
Student ID: 234
Course: B.Com
Student ID: 123
Department: Commerce
Screen Short:
Result:
Thus the program has been successfully executed and
verified.