Inheritance
Inheritance
Ankit.
// 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);
}
}
// 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
}
}
// 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");
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.