Lecture - 7 Inheritance and Method Overridings
Lecture - 7 Inheritance and Method Overridings
Contact Info:
Room No: BS-04, CED
1 Email: ulaila@ssuet.edu.pk
Course Books
Text Book:
Herbert Schildt, Java: The Complete Reference, 12th
Edition ,McGraw-Hill Education.
Deitel, Paul, Java How to Program, 11th Edition, Pearson,
2017
Reference Books:
Horton, Ivor, Beginning Java, 7th Edition, Wrox, 2011
2
Course Instructors
20
30
Inheritance
Method Overriding with example
5
6 Inheritance
Object
Class Animal
Class spaniel
Inheritance Hierarchy
Classes higher up in the hierarchy are more generalized, as
they abstract the class behavior.
Classes lower down in the hierarchy are more specialized, as
they customize the inherited behavior by additional properties
and behavior
Inheritance defines the relationship is-a (also called the
superclass–subclass relationship) between a superclass and
its subclasses.
This means that an object of a subclass can be used wherever
an object of the superclass can be used
A class in Java can only extend one other class; that is, it can
only have one immediate superclass. This kind of inheritance is
sometimes called single or linear implementation
inheritance.
An object of the Dog class is-an object of the superclass Animal
inheritance relationship is transitive: if class B extends class A,
then a class C, which extends class B, will also inherit from class
A via class B.
The is-a relationship does not hold between peer classes.
11
Inheritance
# Example 01
class ParentClass{
public int parentVariable = 10;
public void parentFunction()
{ System.out.println( "Parent Function" ); }
}
Output:
Parent Function
In Child 10
Parent Function 10
13 SuperClass Members
class Human{
//Overridden method
public void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human{
//Overriding method
public void eat(){
System.out.println("Boy is eating");
}
public static void main( String args[]) {
Boy obj = new Boy();
//This will call the child class version of eat()
obj.eat();
}
}
20 Method Overriding
# Example 01
Output:
Boy is eating
21 Method Overriding
# Example 02
This is called
class Animals{
public void sound(){
overriding a method
System.out.println("This is parent class."); Method sound in
}
} Dog overrides
class Dogs extends Animals{ method sound in
public void sound(){
System.out.println("Dogs bark");
Animal
} A subclass variable
}
class m{ can shadow a
public static void main(String[] args){ superclass variable,
Dogs d = new Dogs();
d.sound();
but a subclass
} method can override
} a superclass method
22 Method Overriding
# Example 02 cont..
Output:
Dogs bark
23 Methods a Subclass Must Override
A subclass must override methods that
are declared abstract in the superclass,
or the subclass itself must be abstract.
an abstract method requires the non-
abstract subclasses to override the
method, in order to provide an
implementation.
24 Why override a method?
}
public static void main(String args[]){
Child whoAmI = new Child();
whoAmI.print();
whoAmI.parentPrint();
System.out.println( whoAmI.name );
System.out.println( ( (Parent ) whoAmI).name );
}
}
32 Inheritance
# Example 03
Output:
1.Child
2.Parent
3.Child
4.Parent
Inheritance: Super keyword
33
# Example 04
class Parent{
public String name = "Parent";
}
class Child extends Parent{
}
class GrandChild extend Child {
String name = "GrandChild"; }
class SuperMain {
# Example 04
Output:
1.Child
2.Parent
Casting and Classes
An instance of a child class can be
assigned to a variable (field) of the
parent class.
If a variable references an object of a subclass, the object
can be cast down to its actual type with an explicit cast. The
explicit cast is required.
A runtime error occurs when explicitly casting an object to a
type that it is not hierarchy
In the code below the cast "(Uncle)
object" is a runtime error because at
that time object holds an instance of the
Child class, which is not of type (or
subclass) of Uncle. An object of type
Parent cannot be cast to type Child.
36 Casting and Classes
Inheritance: Casting
37
# Example 05
class Parent{
int data;
}
class Child extends Parent{
String name;
}
class Uncle {
String rich;
}
class Casting {
Object object;
Parent parent;
Child child = new Child();
Uncle uncle;
parent = child;
object = child;
parent = (Parent) object; // explicit cast down
child = (Child) object; // explicit cast down
uncle = (Uncle) object; //Runtime exception }
}
38 Inheritance
# Example 05
Output:
java.lang.ClassCastException: Child:
cannot cast to Uncle
Constructors and Inheritance
Before the constructor in a Child class is called, its
parent's constructor will be called
If the Child class constructor does not do this, the
parent's constructor with no arguments will be
implicitly called.
super( arg list) as the first line in a constructor
explicitly calls the parent's constructor with the
given signature If
1. Parent class implements a constructor with
arguments & Parent class does not implement
a constructor with no arguments
2. Then the Child constructors must eventually
explicitly call a parents constructor
40 Inheritance: Implicit Call to Parent
# Constructor
Example 06
class ParentClass{
//Parent class constructor
ParentClass(){
System.out.println("Constructor of Parent");
}
}
class JavaExample extends ParentClass{
JavaExample(){
/* It by default invokes the constructor of parent class
* You can use super() to call the constructor of parent.
* It should be the first statement in the child class
* constructor, you can also call the parameterized constructor
* of parent class by using super like this: super(10), now
* this will invoke the parameterized constructor of int arg
*/
System.out.println("Constructor of Child");
}
public static void main(String args[]){
//Creating the object of child class
new JavaExample();
}
}
41 Inheritance
# Example 06
Output:
Constructor of Parent
Constructor of Child
42 Inheritance: Explicit Call to Parent
Constructor
# Example 07
class Parent{
//Parent class constructor
public Parent( ) {
System.out.println( "In Parent, No Argument" );
}
public Parent( String message ) {
System.out.println( "In Parent" + message );
}
}
class Child extends Parent {
//Child class constructor
public Child( String message, int value )
{ this( message ); // if occurs must be first
System.out.println( "In Child" ); }
public Child( String message )
{ super( message ); // if occurs must be first
System.out.println( "In Child" + message ); }
Class Constructors
public static void main(String args[]){
System.out.println( "Construct Child" );
Child care = new Child( ">Start from Child<", 5 ); }
}
43 Inheritance
# Example 07
Output: