1) Explain Inheritance in Java Inheritance
1) Explain Inheritance in Java Inheritance
Inheritance is one of the core principles of Object-Oriented Programming (OOP) that allows a new class (subclass or child
class) to inherit the properties and behaviors (fields and methods) of an existing class (superclass or parent class). It enables
code reusability and establishes a natural hierarchical relationship between classes.
Key Points:
extends Keyword: A child class inherits from a parent class using the extends keyword.
Reusability: Code from the parent class can be reused in the child class, reducing redundancy.
Method Overriding: A subclass can override methods of the parent class to provide specific implementations.
Example:
java
Copy code
class Animal {
void sound() {
@Override
void sound() {
System.out.println("Dog barks");
In this example, the Dog class inherits the sound() method from the Animal class and overrides it to
provide a specific behavior.
Example:
java
Copy code
import java.util.Scanner;
System.out.println("Enter 5 integers:");
numbers[i] = scanner.nextInt();
System.out.println(numbers[i]);
scanner.close();
Explanation:
The user is prompted to input 5 integers, which are stored in the array.
The entered integers are then printed using a simple for loop.
1. Execution of Bytecode: The JVM reads and executes the compiled bytecode generated by
the Java compiler.
2. Memory Management: The JVM manages memory allocation and deallocation through a
mechanism called Garbage Collection.
3. Platform Independence: Java code is compiled into bytecode, which can be run on any
device with a JVM, making Java platform-independent.
Components of JVM:
Runtime Data Area: Memory areas used by the JVM, including the heap, stack, method area,
and more.
Execution Engine: Contains the interpreter and JIT (Just-In-Time) compiler for executing
bytecode.
JVM Architecture:
sql
Copy code
+-----------------------------+
| ClassLoader |
+-----------------------------+
+-----------------------------+
| Execution Engine |
+-----------------------------+
+-----------------------------+
Polymorphism:
Polymorphism is the ability of a single entity (method or object) to take multiple forms. In Java, polymorphism is primarily
achieved through method overloading and method overriding.
1. Compile-time Polymorphism (Method Overloading):
o Multiple methods can have the same name but different parameters.
o The method that gets called is determined at compile time based on the method signature.
Example:
java
Copy code
class MathOperations {
return a + b;
return a + b + c;
o When a subclass provides a specific implementation of a method that is already defined in its
superclass.
Method Overriding:
Overriding is a form of runtime polymorphism where a subclass provides its own implementation of a method that is
inherited from the parent class. The method signature (name and parameters) remains the same, but the behavior can be
changed.
Example:
java
Copy code
class Animal {
void sound() {
@Override
void sound() {
System.out.println("Cat meows");
}
}
Key Differences:
Overriding is runtime polymorphism (same method name, same parameters, different class).
Summary:
Inheritance allows one class to inherit fields and methods from another.
Arrays in Java can take inputs and display outputs using loops.
JVM executes Java bytecode and handles memory management and class loading.
Polymorphism enables methods to take different forms, with overloading at compile time
and overriding at runtime.