Professional Documents
Culture Documents
OOP Chap 3 Deber Markose
OOP Chap 3 Deber Markose
OOP Chap 3 Deber Markose
Burie Campus
Department of Computer Science
Lecture 3:
Inheritance and Polymorphism
Ayisheshim Almaw
ayishu165@gmail.com
1
outlines
Inheritance
Casting
Polymorphism
Method Overriding and Overloading
Super
The Object Class
Abstract Classes
Interfaces
Using Interfaces
Inheritance
• Inheritance in java is a mechanism in which one object acquires all the
properties and behaviours of another object.
• Inheritance provided mechanism that allowed a class to inherit property of
another class.
• When a Class extends another class it inherits all non-private members
including fields and methods
• Inheritance represents the IS-A relationship, also known as parent-
child relationship.
• extends keyword is used to describe inheritance in Java.
4
Why use inheritance in java
For Method Overriding (so runtime polymorphism can be achieved).
For Code Reusability.
Syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
Types of Inheritance
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance (Not supported by java)
4. Hierarchical Inheritance
5. Hybrid Inheritance
Single Inheritance
• In single inheritance, one class inherits the properties of another.
• It enables a derived class to inherit the properties and behavior
from a single parent class.
Class A
{
---
}
Class B extends A {
---
}
Example:Single Inheritance
class Teacher {
void teach() {
System.out.println("Teaching subjects");
}}
class Students extends Teacher {
void listen() {
System.out.println("Listening to teacher");
}}
class CheckForInheritance {
public static void main(String args[]) {
Students s1 = new Students();
s1.teach();
s1.listen();
}}
Multilevel Inheritance:
• When a class is derived from a class which is also derived from
another class, i.e.
• a class having more than one parent class but at different levels,
such type of inheritance is called Multilevel Inheritance.
If we talk about the flowchart,
class B inherits the properties and behavior of class A and
class C inherits the properties of class B.
Here A is the parent class for B and class B is the parent
class for C.
So in this case class C implicitly inherits the properties and
methods of class A along with Class B. That’s what is
multilevel inheritance.
Class A{
---
}
Class B extends A{
---
}
Class C extends B{
---
}
• Example : Multilevel Inheritance: class CheckForInheritance {
class Teacher { public static void main(String
void teach() { argu[]) {
homeTution h = new himeTution();
System.out.println("Teaching subject"); h.explains();
}} d.teach();
class Student extends Teacher { d.listen();
}
void listen() { }
System.out.println("Listening");
}}
class homeTution extends Student {
void explains() {
System.out.println("Does homework");
}}
Hierarchical inheritance
class Demo{
public void get(String name, int id)
System.out.println("Id is :"+ id);
{
System.out.println("Name is :"+ name);
System.out.println(“Name is :"+ name); }}
System.out.println(" Id is :"+ id); class MethodDemo8{
} public static void main (String[] args) {
Demo obj = new Demo();
public void get(int id, String name) obj.get(“Ayishu", 1);
{ obj.get(“Almaw", 2);
}}
In Java, we can overload the main() method using different number and types
of parameter but the JVM only understand the original main() method.
Method Overriding in Java
Upcasting in Java
When Parent class reference
variable refers to Child class
object, it is known as
Upcasting.
• The Object class is the parent class of all the classes in java by
default. In other words, it is the topmost class of java.
• The Object class is beneficial if you want to refer any object whose
type you don't know.
• Let's take an example, there is getObject() method that returns an
object but it can be of any type like Employee,Student etc, we can
use Object class reference to refer that object. For example:
• Object obj=getObject();//we don't what object would be returned
from this method
• The Object class provides some common behaviours to all the
objects such as object can be compared, object can be cloned,
object can be notified etc.
Abstraction
• Abstraction is a process of hiding the implementation details
and showing only functionality to the user. Or
• show only important things to the user and hides the internal
details.
• for example sending sms, you just type the text and send the
message. You don't know the internal processing about the
message delivery.
• Abstract way to show essential details (what) to the user and hide non-
essential details (how).
• Advantages/Benefits of Abstraction:
– Only show essential details to end user.
– Hide complexity.
• There are two ways to achieve abstraction in java
1. Abstract class (0 to 100%)
Abstract Classes and Methods
• Let us take example of a TV real example. You press change channel button of TV remote
and channel is changed. Here remote acts as an interface between you and TV.
• An interface is a collection of abstract methods.
• The interface in java is a mechanism to achieve fully abstraction
• Interfaces are syntactically similar to classes, but you cannot create instance of
an Interface and their methods are declared without any body.
• Advantages of Interface
– It Support multiple inheritance
– It helps to achieve abstraction
• When an interface inherits another interface extends keyword is used whereas class
use implements keyword to inherit an interface.
41
• The general form of a class that includes the implements clause looks
like this:
modifier class classname [extends superclass] [implements interface
[interface...]
{
// class-body
}
• Here, modifier is either public or not used.
• If a class implements more than one interface, the interfaces are
separated with a comma.
• If a class implements two interfaces that declare the same method, then
the same method will be used by clients of either interface.
• The methods that implement an interface must be declared public.
• Also, the type signature of the implementing method must match
exactly the type signature specified in the interface definition.
42
// Interface
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void sleep(); // interface method (does not have a body)
}
// Cat "implements" the Animal interface
class Cat implements Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The Cat says: meew meew");
}
public void sleep() { class Main {
public static void main(String[] args) {
// The body of sleep() is provided here
Cat my Cat = new Cat(); // Create a Pig
System.out.println("Zzz"); object
}} my Cat.animalSound();
my Cat.sleep();
}}
Extending Interfaces
• An interface can extend another interface, similarly to the way
that a class can extend another class.
• The extends keyword is used to extend an interface, and the child
interface inherits the methods of the parent interface.
• The following Sports interface is extended by Hockey and
Football interfaces.
Extending Multiple Interfaces:
• A Java class can only extend one parent class. Multiple
inheritance is not allowed. Interfaces are not classes, however,
and an interface can extend more than one parent interface.
• The extends keyword is used once, and the parent interfaces are
declared in a comma-separated list.
• For example, if the Hockey interface extended both Sports and
Event, it would be declared as:
public interface Hockey extends Sports, Event 44
Multiple inheritance in Java by interface
• If a class implements multiple interfaces, or an interface extends
multiple interfaces i.e. known as multiple inheritance.
46
• End of Ch.3
Questions, Ambiguities, Doubts, … ???