0% found this document useful (0 votes)
3 views3 pages

Java Inheritance and Polymorphism Explained

The document explains key concepts of inheritance in Java, including single-level and multilevel inheritance, method overriding, dynamic method dispatch, and the use of the 'super' keyword. It also covers abstract classes, final classes, and interfaces for achieving multiple inheritance. Real-world analogies and code examples are provided to illustrate these concepts.

Uploaded by

pruthvikaranika
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)
3 views3 pages

Java Inheritance and Polymorphism Explained

The document explains key concepts of inheritance in Java, including single-level and multilevel inheritance, method overriding, dynamic method dispatch, and the use of the 'super' keyword. It also covers abstract classes, final classes, and interfaces for achieving multiple inheritance. Real-world analogies and code examples are provided to illustrate these concepts.

Uploaded by

pruthvikaranika
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

1. What is Inheritance in Java?

Explain its significance with a real-world


analogy. (L2)
Inheritance in Java is a mechanism where one class (child/subclass) acquires the properties and
behaviors of another class (parent/superclass). It promotes code reusability and establishes a
hierarchical relationship between classes.
Real-world Analogy: A ‘Car’ inherits features from its parent class ‘Vehicle’. Just like all cars have
wheels and engines (common in all vehicles), they may also have unique features like sunroofs or
air conditioning.

2. Difference between Single-level and Multilevel Inheritance. (L2)


Single-level Inheritance: A child class inherits directly from a single parent class.
class Parent {
void show() { [Link]("Parent class"); }
}
class Child extends Parent {
void display() { [Link]("Child class"); }
}
public class Main {
public static void main(String[] args) {
Child obj = new Child();
[Link]();
[Link]();
}
}

Multilevel Inheritance: A class inherits from another derived class, forming a chain.
class GrandParent {
void greet() { [Link]("GrandParent"); }
}
class Parent extends GrandParent {
void speak() { [Link]("Parent"); }
}
class Child extends Parent {
void play() { [Link]("Child"); }
}
public class Demo {
public static void main(String[] args) {
Child c = new Child();
[Link]();
[Link]();
[Link]();
}
}

3. What is Method Overriding? (L3)


Method overriding occurs when a subclass provides a specific implementation of a method already
defined in its superclass.
class Vehicle {
void start() { [Link]("Vehicle starts"); }
}
class Car extends Vehicle {
void start() { [Link]("Car starts with a key"); }
}
public class Test {
public static void main(String[] args) {
Vehicle v = new Car();
[Link]();
}
}

4. How does Dynamic Method Dispatch work in Java? (L3)


Dynamic Method Dispatch is a mechanism by which a call to an overridden method is resolved at
runtime, not compile time. It enables runtime polymorphism.
class Animal {
void sound() { [Link]("Animal sound"); }
}
class Dog extends Animal {
void sound() { [Link]("Dog barks"); }
}
public class Demo {
public static void main(String[] args) {
Animal a = new Dog(); // Upcasting
[Link](); // Calls Dog's version at runtime
}
}

5. Purpose of super keyword. (L3)


The 'super' keyword is used to call the parent class constructor or access parent class methods and
variables.
class Parent {
Parent() { [Link]("Parent Constructor"); }
void display() { [Link]("Parent Method"); }
}
class Child extends Parent {
Child() {
super();
[Link]("Child Constructor");
}
void show() {
[Link]();
}
}
public class Demo {
public static void main(String[] args) {
Child c = new Child();
[Link]();
}
}

6. When are constructors executed in an inheritance hierarchy? (L2)


Constructors are executed from the parent class to the child class in the order of inheritance.
class A {
A() { [Link]("A constructor"); }
}
class B extends A {
B() { [Link]("B constructor"); }
}
class C extends B {
C() { [Link]("C constructor"); }
}
public class Demo {
public static void main(String[] args) {
new C();
}
}

7. What is an abstract class? (L2)


An abstract class is a class declared with the 'abstract' keyword. It can have abstract
(unimplemented) methods and concrete methods. It cannot be instantiated and is used when
subclasses should define specific implementations.

8. Java Program with Abstract Class Shape. (L3)


abstract class Shape {
abstract double calculateArea();
abstract double calculatePerimeter();
}
class Circle extends Shape {
double radius;
Circle(double r) { radius = r; }
double calculateArea() { return [Link] * radius * radius; }
double calculatePerimeter() { return 2 * [Link] * radius; }
}
class Triangle extends Shape {
double a,b,c;
Triangle(double a,double b,double c){this.a=a;this.b=b;this.c=c;}
double calculateArea(){
double s=(a+b+c)/2;
return [Link](s*(s-a)*(s-b)*(s-c));
}
double calculatePerimeter(){ return a+b+c; }
}
public class TestShape {
public static void main(String[] args){
Shape s1=new Circle(5);
Shape s2=new Triangle(3,4,5);
[Link]("Circle Area: "+[Link]());
[Link]("Triangle Perimeter: "+[Link]());
}
}

9. Can an abstract class have a constructor? (L2)


Yes, abstract classes can have constructors. These constructors are called when a subclass object
is created to initialize inherited data members.

10. What happens if a class is declared final? (L2)


A final class cannot be inherited. It is used to prevent inheritance for security or design reasons.

11. Effect of declaring a method as final. (L2)


If a method is declared final, it cannot be overridden by subclasses.

12. How can interfaces achieve multiple inheritance in Java? (L3)


Java allows a class to implement multiple interfaces, thus achieving multiple inheritance of type.
interface A { void show(); }
interface B { void display(); }
class C implements A, B {
public void show() { [Link]("Show from A"); }
public void display() { [Link]("Display from B"); }
}
public class Demo {
public static void main(String[] args) {
C obj = new C();
[Link]();
[Link]();
}
}

13. Interface Resizable Example. (L3)


interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
}
class Rectangle implements Resizable {
int width, height;
public void resizeWidth(int w) { width = w; }
public void resizeHeight(int h) { height = h; }
void show() { [Link]("Width: "+width+", Height: "+height); }
}
public class Demo {
public static void main(String[] args) {
Rectangle r = new Rectangle();
[Link](10);
[Link](20);
[Link]();
}
}

You might also like