Title: What is inheritance
1What is inheritance?
2Inheritance
Natural, hierarchical way of organizing things.
(superclass)
Animal
(subclasses of Animal)
Dog
Cat
Biped
(subclass of )
Retriever
Guard
Lion
Tiger
Human
(subclass of )
Labrador
German Shepherd
Think in terms of is a relationships An Lion
is a Cat, and so is a Tiger An Labrador is a
Retriever is a Dog is a Animal A Human is a(n)
Biped is a Animal.
3Another hierarchy
Light
Artificial Light
Natural Light
Light Bulb
Tube Light
SpotLight Bulb
NeonLight
- Inheritance defines the relationship is-a (also
called superclass-subclass - relationship) between a superclass and a
subclass. - Classes higher up are more generalized as they
abstract the class behavior. - Classes lower down are more specialized as they
customize the inherited - (abstract) behavior using additional properties
and behavior.
4Inheritance - Overview
Class A
- Class B inherits from Class A, or
- Class B extends Class A
- Class A is the more general form while Class B is
a specialization.
Class B
Class B contains
- All of the properties of A
- Its own methods, not found in A (EXTENDING class
A) - Methods of A that were redefined in B.
(OVERRIDING)
Class A
Class B must belong to the same family as A. You
must be able to say
B is a A
5The Java Object class
All classes in Java Inherit from the Object
class Any object is-a(n) Object The Object class
is always the root of any inheritance hierarchy
6An simple example of Inheritance
Reason possible
Foo f new Foo() System.out.println(f.getClass()
) System.out.println(f.toString())
inheritance inheritance
Java console
java.lang.Foo this is foo
public class Foo public String toString()
return this is foo
UML notation for inheritance between classes.
The arrow originates are the class that is
inheriting (the child).
7Method Overriding
If a class, referred to as Child, inherits from
another class, referred to as Parent, and if the
Parent has a non-static,non-private, non-final
method and the Child also has an identical
method, then the Childs method will replace
(Override) that of the Parent in a Child
object. This characteristic of replacing is
called Method Overriding.
public class Test public main(String
args) Child c new Child()
c.foo()
public class Parent public void foo()
System.out.println(I am a parent)
public class Child extends Parent public
void foo() System.out.println(I am a
child)
Reason Method Overriding
Output I am a child
Warning Dont get Overriding and Overloading
mixed up!
8Method Overriding
- A subclass may override non-static, non-private,
and non-final methods inherited - from a superclass. (So that also means that any
static, private, or final method - cannot be overridden by a subclass.)
- When a method is invoked on an object of the
subclass, it is the new method - definition in the subclass that is executed.
- The new method definition in the subclass must
have the same method - signature (i.e. method name and parameters), and
the same return. - The new definition cannot narrow the
accessibility of the method, but it - can widen it.
- The new method definition can only specify all or
a subset of the exception - classes specified by the throws clause of the
overridden method. - A subclass can use the keyword super to invoke
an overridden method in the - superclass.
9Overriding verses Overloading - what's the
difference?
- Method overriding requires the same method
signature, the same return type - and the original method was inherited from its
superclass. - Overloading requires different method signatures,
but the method name should - be the same.
- (To overload, the parameters must be different in
type and/or number. The - return type is not a part of the signature,
changing it is not enough to - overload methods.)
- A method can be overloaded in the class it is
defined in, or in a subclass. - To invoke an overridden method in the superclass
from a subclass, requires the - use of super.
10Visibility Modifiers
public A method/variable that is public is
callable/accessible from within and out side of
the current object
private A method/variable that is private is
callable/accessible from only within the current
object only.
protected A method/variable that is public is
callable/accessible from within the
current object and all of the subclasses.
11Visibility Modifiers ...
public class Parent protected int a private
int b public int c protected void
pMethod1() System.out.println(Parentmethod1
) private void pMethod2()
System.out.println(Parentmethod2)
public void pMethod3() pmethod2()
pmethod1()
public class Child extends Parent public void
cMethod1() pMethod1() pMethod2()
//ALLOWED? pMethod3() println(a)
//ALLOWED? println(b) //ALLOWED?
public class Client public static void
main() Child c new Child()
c.cMethod1() c.pMethod1()
c.pMethod2() c.pMethod3() println(a)
//ALLOWED?
A childs method that overrides a parent method
cannot narrow the methods visibility
public method protected method private method -
no client access public method - public access