0% found this document useful (0 votes)
13 views7 pages

Java Module 4 Answers

The document outlines the basics of Java programming, focusing on inheritance types, the use of the super keyword, method overriding, final classes and methods, dynamic method dispatch, and abstract classes. It explains single, multilevel, hierarchical, and hybrid inheritance, along with key points about each concept. Additionally, it provides syntax examples and highlights important rules related to Java inheritance and polymorphism.

Uploaded by

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

Java Module 4 Answers

The document outlines the basics of Java programming, focusing on inheritance types, the use of the super keyword, method overriding, final classes and methods, dynamic method dispatch, and abstract classes. It explains single, multilevel, hierarchical, and hybrid inheritance, along with key points about each concept. Additionally, it provides syntax examples and highlights important rules related to Java inheritance and polymorphism.

Uploaded by

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

BASICS OF JAVA PROGRAMMING

BPLCK105C
MODULE 4

1.Types of Inheritance in Java


Definition:
Inheritance is a mechanism where one class (child) acquires properties and
behavior from another class (parent)

Types of Inheritance & Syntax:

1. Single Inheritance
A subclass inherits from a single parent class.

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

class Child extends Parent {


void display() { System.out.println("This is Child class"); }
}

2. Multilevel Inheritance
A class inherits from another class, which is itself inherited from another.

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

class Parent extends Grandparent {


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

class Child extends Parent {


void message() { System.out.println("Child class"); }
}

3. Hierarchical Inheritance
Multiple child classes inherit from the same parent class.
class Parent {
void show() { System.out.println("Parent class"); }
}

class Child1 extends Parent {


void display() { System.out.println("Child1 class"); }
}

class Child2 extends Parent {


void message() { System.out.println("Child2 class"); }
}

4. Hybrid Inheritance
A combination of multiple types of inheritance using interfaces.

interface A {
void show();
}

interface B {
void display();
}

class C implements A, B {
public void show() { System.out.println("Implementing A"); }
public void display() { System.out.println("Implementing B"); }
}

Key Points:
✔ Java does not support multiple inheritance with classes to avoid ambiguity.
✔ Hybrid inheritance is achieved using interfaces.
✔ Private members are not inherited by subclasses.

2. Uses of super Keyword

Definition:
The super keyword is used to access parent class methods,
constructors, and variables inside a subclass.
Syntax:
class Parent {
int a = 10;
void show() { System.out.println("Parent method"); }
}

class Child extends Parent {


void display() {
super.show(); // Calls Parent's method
System.out.println("Value of a: " + super.a); // Access
Parent's variable
}
}

Example :

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

class Child extends Parent {


Child() {
super(); // Calls Parent constructor
System.out.println("Child Constructor");
}
}

public class Test {


public static void main(String[] args) {
new Child();
}
}

Key Points:
✔ super() must be the first statement in a constructor.
✔ Used to access parent class members when they are hidden
by the child class.

3.Superclass Reference to Subclass Object

Definition:
A superclass variable can reference a subclass object, but it can
only access members of the superclass.
Syntax:
Parent obj = new Child(); // Superclass reference to subclass
object

Example :

class Parent {
int x = 10;
}

class Child extends Parent {


int y = 20;
}

public class Test {


public static void main(String[] args) {
Parent obj = new Child();
System.out.println(obj.x); // Allowed
// System.out.println(obj.y); // ERROR: y is not in
Parent
}
}

Key Points:
✔ Can be used for runtime polymorphism.
✔ Cannot access subclass-specific members unless typecasting
is done.

4.Method Overriding

Definition:
Method overriding occurs when a subclass provides a specific implementation
of a method that is already defined in its parent class.

Syntax:

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

class Child extends Parent {


@Override
void show() { System.out.println("Child Method"); } //
Overridden method
}

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

class Child extends Parent {


void show() { System.out.println("Child Method"); }
}

public class Test {


public static void main(String[] args) {
Parent obj = new Child();
obj.show(); // Calls Child's show()
}
}

Key Points:
✔ The method name and parameters must be the same.
✔ Cannot override final methods.

5.final Class & final Method

Definition:
 Final Class: A class that cannot be inherited.
 Final Method: A method that cannot be overridden.
Syntax:
final class Parent {
final void show() { System.out.println("Final
Method"); }
}

Example :
final class Parent {
void show() { System.out.println("Parent Method"); }
}
// class Child extends Parent {} // ERROR: Cannot extend final class

Key Points:
✔ final prevents modification of class or method.
✔ Helps in performance optimization.

6.Dynamic Method Dispatch

Definition:
Dynamic method dispatch (runtime polymorphism) allows
method calls to be resolved at runtime based on the actual
object type.
Syntax:
Parent obj = new Child(); // Runtime polymorphism
obj.method();

Example :
class Parent {
void display() { System.out.println("Parent Display"); }
}

class Child extends Parent {


void display() { System.out.println("Child Display"); }
}

public class Test {


public static void main(String args[]) {
Parent obj = new Child();
obj.display(); // Calls Child's method at runtime
}
}

Key Points:
✔ Java determines method execution at runtime.
✔ Used in achieving polymorphism.

7.Abstract Class
Definition:
An abstract class cannot be instantiated and may have abstract methods that
must be implemented by subclasses.
Syntax:

abstract class ClassName {


abstract void methodName(); // Abstract method
}

Example :
abstract class Animal {
abstract void makeSound();
}

class Dog extends Animal {


void makeSound() { System.out.println("Bark"); }
}

public class Test {


public static void main(String args[]) {
Animal obj = new Dog();
obj.makeSound();
}
}

Key Points:
✔ Used when we want a base class but don't want direct object
creation.
✔ Must be extended and its abstract methods must be
implemented.

SACHIN (1ST SEM)


DATA SCIENCE
ACET

You might also like