How is Nested Class different from Inheritance?
How is Nested Class different from Inheritance?
Read lessSign up to our innovative Q&A platform to pose your queries, share your wisdom, and engage with a community of inquisitive minds.
Log in to our dynamic platform to ask insightful questions, provide valuable answers, and connect with a vibrant community of curious minds.
Forgot your password? No worries, we're here to help! Simply enter your email address, and we'll send you a link. Click the link, and you'll receive another email with a temporary password. Use that password to log in and set up your new one!
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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