07 Inheritance
07 Inheritance
// output
Employee [name=Asma, salary=100.0]
Programmer [name=Rawan, salary=200.0, product=HR system]
SiteEngineer [name=Amal, salary=200.0, siteLocation=Dubai]
package p1;
© All rights reserved.
public class C1 { public class C2 {
public int x; C1 o = new C1();
protected int y; can access o.x;
int z; can access o.y;
private int u; can access o.z;
cannot access o.u;
protected void m() {
Abstract Classes
• Java allows declaring a class as an abstract class
• Abstract classes cannot be instantiated
• Abstract classes are meant to be superclasses
• Abstract classes can have constructors, which are meant to be used by
subclasses
• Abstract classes may contain abstract methods, which are implemented
in subclasses
• Abstract methods can only be defined inside Abstract classes
// output
meaw
play with yarn
hunting mice
bark
play fetch
barking at strangers
// output
meaw
play with yarn
bark
play fetch
public class B {
public final void C {
… Subclasses of B are NOT allowed to override method C
}
}
Bird Rocket
-species: String
-color: String -name: String
// output
Fly with wings
Fly with engine
• A class
an interface may implement
is different from definingmore than
an abstract class.one
Tableinterface buttheit
13.2 summarizes can extends only
differences.
one superclass