0% found this document useful (0 votes)
1 views

Inheritance Interface Java (1)

The document covers the concept of inheritance in programming, defining it as a mechanism for deriving new classes from existing ones, with types including single, multilevel, hierarchical, and hybrid inheritance. It explains the use of the 'final' keyword to prevent further inheritance and provides examples of multilevel inheritance, single inheritance, hierarchical inheritance, method overriding, constructor overloading, and the use of the super keyword. Additionally, it discusses dynamic method dispatch, illustrating how the actual object's method is called at runtime.

Uploaded by

d45127052
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Inheritance Interface Java (1)

The document covers the concept of inheritance in programming, defining it as a mechanism for deriving new classes from existing ones, with types including single, multilevel, hierarchical, and hybrid inheritance. It explains the use of the 'final' keyword to prevent further inheritance and provides examples of multilevel inheritance, single inheritance, hierarchical inheritance, method overriding, constructor overloading, and the use of the super keyword. Additionally, it discusses dynamic method dispatch, illustrating how the actual object's method is called at runtime.

Uploaded by

d45127052
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Unit - II: Inheritance, Interface and Packages (12 Marks)

Inheritance

1. Define Inheritance and List its types:

Inheritance is a mechanism wherein a new class is derived from an existing class. Types:

- Single Inheritance

- Multilevel Inheritance

- Hierarchical Inheritance

- Hybrid Inheritance (via Interfaces)

2. State the use of final keyword with respect to inheritance:

The 'final' keyword prevents further inheritance. A final class cannot be extended, and a final method

cannot be overridden.

3. Program to Demonstrate Multilevel Inheritance:

class A {

void displayA() { System.out.println("Class A"); }

class B extends A {

void displayB() { System.out.println("Class B"); }

class C extends B {

void displayC() { System.out.println("Class C"); }

public class Main {

public static void main(String[] args) {

C obj = new C();

obj.displayA(); obj.displayB(); obj.displayC();


}

4. Explain Single and Multilevel Inheritance with example:

Single: One class inherits from another.

Multilevel: A class inherits from a derived class.

5. Program for class Book and BookInfo:

class Book {

String author, title; double price;

Book(String a, String t, double p) {

author = a; title = t; price = p;

class BookInfo extends Book {

int stock;

BookInfo(String a, String t, double p, int s) {

super(a, t, p); stock = s;

void display() {

System.out.println(title + " by " + author + ", Rs." + price + ", Stock: " + stock);

6. Program for Hierarchical Inheritance:

class A { void msg() { System.out.println("A"); } }

class B extends A { void msgB() { System.out.println("B"); } }


class C extends A { void msgC() { System.out.println("C"); } }

7. Method Overriding Example:

class Parent { void show() { System.out.println("Parent"); } }

class Child extends Parent { void show() { System.out.println("Child"); } }

8. Constructor Overloading Example:

class Demo {

Demo() { }

Demo(int x) { System.out.println(x); }

9. Use of super keyword:

- Call parent class constructor

- Access parent class methods/variables

10. Dynamic Method Dispatch:

Parent obj = new Child(); obj.show();

Method of the actual object (Child) is called at runtime.

You might also like