0% found this document useful (0 votes)
1 views4 pages

Example Inheritance

The document provides examples of inheritance, method overloading, and wrapper classes in Java. It illustrates how a child class can inherit methods from a parent class, how methods can be overloaded with different parameters, and how primitive types can be converted to their corresponding wrapper classes. Additionally, it includes a summary table of access modifiers and their visibility in different contexts.

Uploaded by

ranajitbarik2005
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)
1 views4 pages

Example Inheritance

The document provides examples of inheritance, method overloading, and wrapper classes in Java. It illustrates how a child class can inherit methods from a parent class, how methods can be overloaded with different parameters, and how primitive types can be converted to their corresponding wrapper classes. Additionally, it includes a summary table of access modifiers and their visibility in different contexts.

Uploaded by

ranajitbarik2005
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/ 4

Example inheritance

// example of inheritence
// Parent class (Super class)
class Animal {
void sound() {
System.out.println("Animals make sound");
}
}

// Child class (Sub class) inherits from Animal


class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}

// Main class to test inheritance


public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound(); // Inherited method
d.bark(); // Child's own method
}
}

// output
Animals make sound
Dog barks

Methode overloading
// Example of Method Overloading
class Calculator {

// Method to add two integers


int add(int a, int b) {
return a + b;
}

// Overloaded method to add three integers


int add(int a, int b, int c) {
return a + b + c;
}
// Overloaded method to add two double values
double add(double a, double b) {
return a + b;
}
}

// Main class to test method overloading


public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();

System.out.println("Sum = " + calc.add(10, 20)); // Calls


method with 2 int parameters
System.out.println("Sum = " + calc.add(5, 10, 15)); // Calls
method with 3 int parameters
System.out.println("Sum = " + calc.add(2.5, 3.5)); // Calls
method with 2 double parameters
}
}

// output
Sum = 30
Sum = 30
Sum = 6.0

wrapper classes
// Example of Wrapper Classes in Java
public class Main {
public static void main(String[] args) {

// Converting primitive int to Integer (Boxing)


int a = 10;
Integer aObj = Integer.valueOf(a); // manual boxing
// or simply: Integer aObj = a; // auto-boxing

// Converting Integer to primitive int (Unboxing)


int b = aObj.intValue(); // manual unboxing
// or simply: int b = aObj; // auto-unboxing

// Using other wrapper classes


double d = 5.5;
Double dObj = d; // auto-boxing

char ch = 'X';
Character chObj = ch; // auto-boxing

System.out.println("Integer object: " + aObj);


System.out.println("Primitive int: " + b);
System.out.println("Double object: " + dObj);
System.out.println("Character object: " + chObj);
}
}

// output
Integer object: 10
Primitive int: 10
Double object: 5.5
Character object: X

Access Modifiers Summary Table


Subclass (Same Subclass (Diff World
Modifier Class Package
Pkg) Pkg) (Anywhere)

public ✅ ✅ ✅ ✅ ✅

protected ✅ ✅ ✅ ✅ ❌

default ✅ ✅ ✅ ❌ ❌

private ✅ ❌ ❌ ❌ ❌
Key Takeaways for Exams
✔ public → Global access.
✔ private → Only within the class (used for encapsulation).
✔ protected → Class + Subclasses + Same Package.
✔ default → Package-private (no keyword).

You might also like