Java_Inheritance_Concepts (1)
Java_Inheritance_Concepts (1)
When a class inherits from another, it gets access to all non-private members (fields and methods).
Private members are not inherited, but can be accessed via public/protected methods.
2. Multilevel Inheritance
A class inherits from another class, which in turn inherits from a third class.
Example:
class A {}
class B extends A {}
class C extends B {}
3. super Keyword
Used to call the parent class constructor or access parent class methods/variables.
Example:
super();
super.name;
super.display();
4. final Keyword
Used to:
Example:
final class A {}
5. Method Overriding
Java Inheritance Concepts - Summary and Program
Example:
void sleep() {
void sound() {
System.out.println("Dog barks");
void showNames() {
void sound() {
System.out.println("Puppy yelps");
Animal a;
a = new Puppy();
a.sound();
a.sleep();
pup.showNames();
pup.eat();