How is Nested Class different from Inheritance?
Aryan ShuklaBeginner
Asked: 8 months ago2025-03-24T08:26:17+05:30
2025-03-24T08:26:17+05:30In: Information Technology
How is Nested Class different from Inheritance?
Share
You must login to add an answer.
Need An Account, Sign Up Here
Related Questions
- If a quad in a K-map appears redundant when grouping ...
- Consider the following Java code: int x = 7896;System.out.println(x + ...
- In Java, consider the following code snippet: Scanner sc = ...
- What is the difference between next() and nextLine()?
- In Java programming sum(5,6) will call for which of these ...
A nested class is not the same as an inherited class. Let’s see why with simple examples. Nested Classes A nested class is just a class declared inside another class. (a) Static Nested Class class School { static class Student { void showDetails() { System.out.println("I am a student of the school."Read more
A nested class is not the same as an inherited class. Let’s see why with simple examples.
A nested class is just a class declared inside another class.
(a) Static Nested Class
class School {
static class Student {
void showDetails() {
System.out.println(“I am a student of the school.”);
}
}
}
public class Demo {
public static void main(String[] args) {
School.Student s = new School.Student();
s.showDetails();
}
}
(b) Inner Class (Non-static)
class School {
class Teacher {
void display() {
System.out.println(“I am a teacher of the school.”);
}
}
}
public class Demo {
public static void main(String[] args) {
School school = new School();
School.Teacher t = school.new Teacher();
t.display();
}
}
Inheritance happens when one class extends another.
class Person {
void displayInfo() {
System.out.println(“I am a person.”);
}
}
class Student extends Person {
void showDetails() {
System.out.println(“I am a student.”);
}
}
public class Demo {
public static void main(String[] args) {
Student s = new Student();
s.displayInfo(); // inherited from Person
s.showDetails(); // defined in Student
}
}
- Student inherits from Person.
- That means Student automatically gets displayInfo().
See less