Java program demonstrating simple inheritance using a base class Student and a derived class
Ankit.
// Base class Student
class Student {
int roll;
int mark;
String name;
// Method to print welcome message
void show() {
System.out.println("Welcome");
}
}
// Derived class Ankit extending Student
class Ankit extends Student {
// Method to assign values and display details
void display() {
roll = 1;
mark = 90;
name = "Ankit";
System.out.println("Roll Number: " + roll);
System.out.println("Name: " + name);
System.out.println("Marks: " + mark);
}
// Main method inside class Ankit
public static void main(String[] args) {
// Creating object of Ankit class
Ankit obj = new Ankit();
// Calling methods
obj.show(); // Calling base class method
obj.display(); // Calling derived class method
}
}
2. A Java program demonstrating multilevel inheritance
// Base class Student
class Student {
int roll;
int mark;
String name;
// Method to print welcome message
void show() {
System.out.println("Welcome to the Student Database");
}
}
// Intermediate class Ankit extends Student
class Ankit extends Student {
// Method to assign values and display details
void display() {
roll = 1;
mark = 90;
name = "Ankit";
System.out.println("Roll Number: " + roll);
System.out.println("Name: " + name);
System.out.println("Marks: " + mark); } }
// Derived class Scholar extends Ankit (Multilevel Inheritance)
class Scholar extends Ankit {
double scholarshipAmount = 5000.0; // Example scholarship amount
// Method to show scholarship details
void scholarship() {
System.out.println("Scholarship Amount: $" + scholarshipAmount);
}
// Main method inside class Scholar
public static void main(String[] args) {
// Creating object of Scholar class
Scholar obj = new Scholar();
// Calling methods from all levels of inheritance
obj.show(); // From Student class
obj.display(); // From Ankit class
obj.scholarship(); // From Scholar class
} }
3. a Java program demonstrating Hierarchical Inheritance
// Base class
class Student {
int roll;
String name;
void show() {
System.out.println("Welcome to Student Portal");
} }
// Derived class 1
class Ankit extends Student {
void display() {
roll = 1;
name = "Ankit";
System.out.println("Roll Number: " + roll);
System.out.println("Name: " + name);
}
}
// Derived class 2
class Rahul extends Student {
void details() {
roll = 2;
name = "Rahul";
System.out.println("Roll Number: " + roll);
System.out.println("Name: " + name);
}
}
// Main class
public class HierarchicalDemo {
public static void main(String[] args) {
Ankit obj1 = new Ankit();
Rahul obj2 = new Rahul();
obj1.show();
obj1.display();
System.out.println(); // Separator
obj2.show();
obj2.details();
} }
4. Java Program Demonstrating super Keyword(Access Variable)
// Parent class
class A {
int a = 10; // Variable in parent class
}
// Child class
class B extends A {
int a = 20; // Variable in child class
void show() {
System.out.println("Value of a in class B: " + a); // Accessing child class
variable
System.out.println("Value of a in class A using super: " + super.a); // Accessing
parent class variable
}
}
// Main class
public class Test {
public static void main(String[] args) {
B obj = new B(); // Creating object of child class B
obj.show(); // Calling show() method
}
}
5. Java Program Demonstrating super Keyword for Method Access
In this example, both Class A and Class B have a method named show(). The super
keyword is used in Class B to call the show() method of Class A.
// Parent class
class A {
void show() {
System.out.println("This is show() method from Class A");
}
}
// Child class
class B extends A {
void show() {
System.out.println("This is show() method from Class B");
super.show(); // Calling the parent class show() method
}
}
// Main class
public class Test {
public static void main(String[] args) {
B obj = new B(); // Creating an object of class B
obj.show(); // Calls the show() method of class B
}
}
6. Java Program Demonstrating super Keyword to Access Default Constructor
// Parent class
class A {
A() { // Default constructor
System.out.println("Default Constructor of Class A");
}
}
// Child class
class B extends A {
B() { // Default constructor of B
super(); // Calling the default constructor of Class A
System.out.println("Default Constructor of Class B");
}
}
// Main class
public class Test {
public static void main(String[] args) {
B obj = new B(); // Creating an object of Class B
}
}
Without super(); in Class B
// Parent class
class A {
A() { // Default constructor
System.out.println("Default Constructor of Class A"); } }
// Child class
class B extends A {
B() { // Default constructor of B
// super(); // This is automatically inserted by the compiler
System.out.println("Default Constructor of Class B");
}
}
// Main class
public class Test {
public static void main(String[] args) {
B obj = new B(); // Creating an object of Class B
} }
7. Parameterized Constructor with super().
// Parent class with parameterized constructor
class A {
int a;
// Parameterized constructor
A(int x) {
this.a = x;
System.out.println("Parameterized Constructor of Class A: a = " + a);
}
}
// Child class extending A
class B extends A {
int b;
// Parameterized constructor of B
B(int x, int y) {
super(x); // Call parent class constructor
this.b = y;
System.out.println("Parameterized Constructor of Class B: b = " + b);
}
}
// Main class
public class Test {
public static void main(String[] args) {
B obj = new B(10, 20); // Passing values to constructor
}
}
8. What is @Override in Java?
@Override is an annotation in Java that indicates a method is overriding a
method from its superclass (parent class). It is part of method overriding, where a
subclass provides a specific implementation of a method already defined in the
parent class.
Example of polymorphism.
a Java program demonstrating polymorphism, where a Person can be a Teacher,
Student, Friend, or Customer.
// Parent class
class Person {
String name;
// Constructor
Person(String name) {
this.name = name;
}
// Method to be overridden
void role() {
System.out.println(name + " is a Person.");
}
}
// Subclass 1: Teacher
class Teacher extends Person {
Teacher(String name) {
super(name);
}
@Override
void role() {
System.out.println(name + " is a Teacher, teaching students.");
}
}
// Subclass 2: Student
class Student extends Person {
Student(String name) {
super(name);
}
@Override
void role() {
System.out.println(name + " is a Student, studying subjects.");
}
}
// Subclass 3: Friend
class Friend extends Person {
Friend(String name) {
super(name);
}
@Override
void role() {
System.out.println(name + " is a Friend, supporting others.");
}
}
// Subclass 4: Customer
class Customer extends Person {
Customer(String name) {
super(name);
}
@Override
void role() {
System.out.println(name + " is a Customer, purchasing items.");
}
}
// Main class
public class PolymorphismExample {
public static void main(String[] args) {
// Using polymorphism (Parent reference pointing to Child objects)
Person p1 = new Teacher("Bishnu");
Person p2 = new Student("Bishnu");
Person p3 = new Friend("Bishnu");
Person p4 = new Customer("Bishnu");
// Calling overridden method
p1.role();
p2.role();
p3.role();
p4.role();
}
}
the output:
Bishnu is a Teacher, teaching students.
Bishnu is a Student, studying subjects.
Bishnu is a Friend, supporting others.
Bishnu is a Customer, purchasing items.