Inheritance-1

Report
Programming With Java
ICS201
Chapter 1
Inheritance
University Of Ha’il
1
Programming With Java
ICS201
Introduction to Inheritance

Inheritance is one of the main techniques of objectoriented programming (OOP)

Using this technique, further classes can be created
from existing ones; those classes are said to inherit
the methods and instance variables of the class they
inherited

Base Class

The original class is called the base class

The new class is called a derived class Derived Class
Advantage: Reusing existing code
7-2
Programming With Java
ICS201
Inheritance
Animal
Cat
Dog
Fox
3
Programming With Java
ICS201
Inheritance
Land Vehicle
Bus
Truck
Car
4
Programming With Java
ICS201
Inheritance
Land Vehicle
Bus
Car
Truck
Toyota
Yaris
Corolla
Camry
5
Programming With Java
ICS201
An Example Inheritance Hierarchy

Data and behavior associated with super-classes
are accessible to those subclasses
6
Programming With Java
ICS201
The is-a Rule
Always
check generalizations to ensure
they obey the is-a rule

“A checking account is an account”

“A village is a municipality”

“A cat is an animal”
7
Programming With Java
ICS201
Derived Class (subclass)

A derived class, also called a subclass (or child class), is
defined by starting with another already defined class,
called a base class or superclass or parent class, and
adding (and/or changing) methods, instance variables,
and static variables

The derived class inherits:


all the public methods,

all the public and private instance variables, and

all the public and private static variables from the base class.
The derived class can add more instance variables,
static variables, and/or methods.
University Of Ha’il
8
Programming With Java
ICS201
Derived Classes

The extends clause in a class declaration establishes an
inheritance relationship between two classes. It has the
following syntax:
class DerivedClass extends BaseClass
{
// body of the class
}
Base Class
Derived Class
University Of Ha’il
9
Programming With Java
ICS201
Parent and Child Classes

A base class is often called the parent class


A derived class is then called a child class
These relationships are often extended such that
a class that is a parent of a parent . . . of another
class is called an ancestor class

If class A is an ancestor of class B, then class B can be
called a descendent of class A
7-10
Programming With Java
ICS201
Inheritance and Variables
Variable Hiding

If a variable of a class has a same name (type maybe
different) as a superclass variable, in that case class
variable hides the superclass variable.

You can access a hidden variable by using super keyword
super.variable
University Of Ha’il
11
Programming With Java
ICS201
Example (Inheritance and variables)
class C1 {
static int x;
}
class C2 extends C1 {
static String x;
}
class Cc {
public static void main(String[] args)
{
C1 p = new C1();
p.x = 55;
System.out.println("p.x=" + p.x);
C2 q = new C2();
q.x = "This is a String";
System.out.println( "q.x=" + q.x);
}
}
University Of Ha’il
Output:
p.x=55
q.x=This is a String
12
Programming With Java
ICS201
Example (Inheritance and variables)
class M100 {
int x = 100;
}
class M200 extends M100 {
String x = " Welcome “ ;
void display()
{
System.out.println( "x=" + x);
System.out.println( "super.x=" +super.x);
}
}
class SuperKeyword {
public static void main(String[] args)
{
M200 m200 = new M200();
m200.display();
}
}
University Of Ha’il
Output:
x= Welcome
super.x=100
13
Programming With Java
ICS201
Example

Inherit1.java

Inherit2.java
University Of Ha’il
14
Programming With Java
ICS201
Homework
Write an application that illustrates how to access a hidden
variable. Class G declares a static variable x. Class H
extends G and declares an instance variable x. A display()
method in H displays both of these variables.
Dr.Mwaffaq Abu Alhija
University Of Ha’il
15
Inheriting Methods
Programming With Java
ICS201


Override method:


Supply a different implementation of a method that
exists in the superclass
Must have same signature (same name and same
parameter types)
Inherit method:


Don't supply a new implementation of a method that
exists in superclass
Superclass method can be applied to the subclass
objects
Programming With Java
ICS201
Inheriting Methods (Cont’d)

Add method:

Supply a new method that doesn't exist in
the superclass

New method can be applied only to subclass
objects
Programming With Java
ICS201
Overloading vs Overriding

Comparison between overloading and overriding
Overriding deals with two
methods, one in a parent
class and one in a child class
with the same signature
Overriding lets you define a
similar operation in different
ways for different object types
Overloading deals with
multiple methods in the same
class with the same name but
different signatures.
Overloading lets you define a
similar operation in different
ways for different data
18
Programming With Java
ICS201
Method Overriding

The access permission of an overridden method can be
changed from private in the base class to public in the
derived class.

However, the access permission of an overridden
method can not be changed from public in the base
class to a more restricted access permission in the
derived class.
University Of Ha’il
19
Programming With Java
ICS201
Method Overriding


Given the following method header in a base case:
private void doSomething()
The following method header is valid in a derived class:
public void doSomething()
However, the opposite is not valid


Given the following method header in a base case:
public void doSomething()
The following method header is not valid in a derived
class:
private void doSomething()
University Of Ha’il
20
Programming With Java
ICS201
Example (Method Overriding)
class A1
{
void hello()
{
System.out.println( "Hello from A1" );
}
}
class B1 extends A1
{
void hello()
{
System.out.println( "Hello from B1" );
}
}
class MethodOverriding
{
public static void main(String[] arg)
{
C1 obj = new C1();
obj.hello();
A1 a = new C1() ;
a.hello();
}
}
class C1 extends B1
{
void hello()
{
System.out.println( "Hello from C1");
}
}
University Of Ha’il
Output:
Hello from C1
Hello from C1
21

similar documents