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

Java Inheritance and Polymorphism Explained

Uploaded by

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

Java Inheritance and Polymorphism Explained

Uploaded by

Dhavan Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Multi-level inheritance:

public class A{
protected int x;
public A(int x){
this.x=x;
}
}

public class B extends A{


protected float y;
public B(int x,float y){
super(x);
this.y=y;
}

public class C extends B{


protected String s;
public C(int x,float y,String s){
super(x,y);
this.s=s;
}
public void show(){
[Link](super.x);
[Link](super.y);
[Link](this.s);
}
public static void main(String[] args){
C c=new C(10,3.4f,"msg");
[Link]();
}
}

Hierarchical Inheritance:

public class Input{


protected int x,y,res;
public Input(){
Scanner sc=new Scanner([Link]);
[Link]("X:");
this.x=[Link]();
[Link]("Y:");
this.y=[Link]();
}
public void show(){
[Link]([Link]);
}
}

public class Addition extends Input{


public Addition(){
super();
}
public void sum(){
[Link]=super.x+super.y;
}
}
public class Subtraction extends Input{
public void sub(){
[Link]=super.x-super.y;
}
}

public class Main{


public static void main(String[] args){
Addition add=new Addition();
Subtraction sub=new Subtraction();
[Link]();
[Link]();
[Link]("Addition:");
[Link]();
[Link]("Subtraction:");
[Link]();
}
}

Binding:
* Process of replacing a function call with function body
Compile-time binding -> method body is replace the function during compile time
Run-time binding -> method body is selected during run-time

Method Overloading:(static polymorphism or compile-time)

public class Exam {


public void show() {
[Link]("Welcome");
}
public void show(String name) {
[Link]("Hi,"+name);
}
public void show(String name,String role) {
[Link]("Hi,"+role+", your name is "+name);
}
}

public class Main {

public static void main(String[] args) {


Exam e=new Exam();
[Link]();// method with 0 parameters
[Link]("Rakesh");
[Link]("Rajesh","Admin");
}

public class A{
int x;
A(){
this.x=0;
}
A(int x){
this.x=x;
}
}

A a=new A(); // will call default constructor


//a.x => 0
A a1=new A(20); // will call param constructor
//a1.x=> 20

Common questions

Powered by AI

Method overloading in the Exam class demonstrates static polymorphism by defining multiple `show` methods with different parameter lists. During compile time, the Java compiler determines which `show` method to call based on the number and type of arguments passed in. For example, `show()` is called without parameters, `show(String name)` is called when a single string is passed, and `show(String name, String role)` is called when two strings are passed. This allows different behaviors without changing the method name .

Multi-level inheritance in the example involves a class hierarchy where class C inherits from class B, which in turn inherits from class A. This chain of inheritance allows class C to access protected and public members of both class B and class A through the `super` keyword. Specifically, class C's constructor uses `super(x, y)` to initialize its superclass B, which in turn uses `super(x)` to initialize class A. The `show()` method in class C accesses and prints the values `x`, `y`, and `s` showing inheritance of properties from classes B and A .

Compile-time binding, also known as static binding, decides the method to invoke at compile-time based on the method signature and overloading, such as determining which variant of an overloaded method to call based on parameter types. Run-time binding, or dynamic binding, defers this decision to run-time, as seen in polymorphism where the overridden method call is determined by the actual object's type, allowing for method overriding behavior seen in inheritance scenarios .

Method overriding occurs when a derived class provides a specific implementation of a method already defined in its base class, enabling run-time polymorphism. The document does not provide an explicit overriding example but outlines class hierarchy usage implying method sharing. In contrast, method overloading, shown in the Exam class, allows multiple methods within the same class to share the same name but differ in parameters, achieving compile-time polymorphism. Overloading focuses on method signature variance, while overriding depends on inheritance for method behavior specialization .

Hierarchical inheritance allows multiple classes to inherit from a single base class. In the example, classes Addition and Subtraction both inherit from the Input class. This design enables the derived classes to use the input fields `x`, `y`, and `res`, as well as the method `show()` from the Input class, while implementing their specific functionalities: `sum()` in Addition and `sub()` in Subtraction. This design facilitates code reuse and logical segmentation of arithmetic operations .

Encapsulation in the Exam class enhances functionality by defining multiple `show` methods with varying parameters to handle different input scenarios without exposing internal logic directly. It maintains method calls without revealing underlying implementation through clear method signatures, offering flexibility and simplicity in interaction. For example, different messages are constructed internally based on provided arguments, keeping data processing internal while maintaining a consistent interface .

Encapsulation in the hierarchical inheritance example is demonstrated through the protected access modifier used for the variables `x`, `y`, and `res` in the Input class, allowing them to be used by derived classes Addition and Subtraction. This design maintains data integrity by managing access through controlled methods such as `show` and ensures that only classes inheriting Input access these fields directly. Thus, encapsulation promotes modularity and data protection in derived class implementations .

The `super` keyword in multi-level inheritance is used to access parent class members and invoke parent class constructors, fostering member inheritance across multiple class levels. In the provided example, class B calls `super(x)` to initialize variables from class A, and class C calls `super(x, y)` to invoke B's constructor. Moreover, `super` allows C's `show` method to output values of `x` and `y` from classes A and B, respectively, illustrating inheritance efficiency .

Constructor overloading in class A illustrates the use of multiple constructors with different signatures to instantiate an object in various states. The default constructor initializes the member `x` to zero, while the parameterized constructor initializes `x` with a given integer. For instance, creating `A a = new A();` calls the default constructor setting `a.x` to 0, while `A a1 = new A(20);` uses the parameterized constructor setting `a1.x` to 20. This allows flexibility and clarity in object instantiation .

Using the `Scanner` class for input in the Input class offers straightforward text parsing for input operations, enabling dynamic reading of integers from user input. It streamlines data acquisition by integrating intuitive console input mechanisms (`nextInt`), thereby simplifying input handling across inherited classes, such as Addition and Subtraction which rely on initialized values to perform calculations. Additionally, `Scanner` manages different data types, promoting code readability and operational efficiency in interactive applications .

You might also like