Week 10
Interfaces:
Abstraction, Multiple inheritance
Dr. Nehad Ramaha,
Computer Engineering Department
Karabük Universities These Slides mainly adopted from Assist. Prof. Dr. Ozacar Kasim lecture notes
1
The class notes are a compilation and edition from many sources. The instructor does not claim intellectual property or ownership of the lecture notes.
Week 10
Reference type casting
Casting from a subclass to a superclass is called upcasting
Cat cat = new Cat(); // class Cat extends Animal
Animal animal = (Animal) cat;
Casting from a superclass to a subclass is called downcasting
Animal animal = new Cat()
((Cat) animal).meow(); // extends the method available to Cat object
Upcasting narrows methods and attributes available to this object,
Downcasting extends methods and attributes available to this object
2
Week 10
Cat cat = new Animal(); //??
Animal a = new Cat();
a.meow(); //??
((Cat)a).meow(); //??
Object o = new Animal();
o.eat(); //??
o.meow(); //??
((Animal)o).eat(); //??
((Animal)o).meow(); //??
((Cat)o).eat(); //?? (*)
((Cat)o).meow(); //??
* It is legal to cast a reference to the wrong subtype;
However, this will compile but crash when the program runs
An interface is like an abstract class that is intended to be
used as a common base to access a number of similarly-
behaving objects.
An interface is a contract an implementing class needs to
respect
interface contains behaviors that a class implements. (Contract)
Methods in an interface are by-default public. (Contract)
4
There are mainly three reasons to use
interface. Using interface we can:
1. achieve abstraction.
2. support the functionality of multiple
inheritance.
3. achieve loose coupling.
2/10/2023 5
We already know
this!
6
An interface is created with the following syntax:
modifier interface InterfaceID {
//constants
//abstract method
}
An interface can extend other interfaces with the following syntax:
modifier interface interfaceID extends interface1, .., interfaceN {
//constants
//abstract method, (After Java 8.0 static and default methods)
}
It provides total abstraction which means
◦ all the methods in an interface are declared with the empty body,
◦ and all the attributes are public, static and final by default.
A class that implements an interface must implement all the methods declared in the interface.
7
1. Interfaces are like abstract classes.
2. An interface is not extended by a class; it is implemented by a
class. But, one interface can extend another one.
3. An interface does not contain any constructors.
4. A class can implement any number of interfaces.
(multiple inheritance)
5. Interfaces cannot be instantiated. (like abstract classes)
6. You can use interface as a data type for variables. (Like classes)
7. Interfaces can contain only abstract methods and constants.
8
Interface Abstract class
Variable Only constants Constants and variable
(attribute) data
Methods No implementation allowed Abstract or concrete
(behaviour) (no abstract modifier necessary)
Interface Abstract class
A subclass can implement many interfaces A subclass can inherit only one class
Can extend numerous interfaces Can implement numerous interfaces
Cannot extend a class Extends one class
11
1
Abstract classes vs. Abstraction
◦ Abstract classes are used for inheritance, which is more heavily used to
achieve code reuse
◦ Abstraction enables large systems to be built without increasing the
complexity of code and understanding.
◦ In other words, abstraction is a process where you show only “relevant”
data and “hide” unnecessary details of an object from the user. We do
abstraction when deciding what to implement.
◦ We achieved abstraction using abstract classes and interfaces.
12
Salary<<interface>> 1
CalculateSalary() declaration
Employee<<abstract>>
CalculateSalary() declaration
name; Employee<<abstract>>
wage;
name;
wage;
FullTimeEmployee PartTimeEmployee
FullTimeEmployee PartTimeEmployee
CalculateSalary() CalculateSalary()
CalculateSalary() hours; CalculateSalary() hours;
Abstraction using Abstract Abstraction using Abstract classes and
classes Interfaces
13
2
A Classes can inherit multiple interfaces. public interface A {
void a();
The advantage of multiple inheritance is }
that it allows a class to inherit the public interface B {
functionality of more than one base class void b();
◦ thus allowing for modeling of complex }
relationships. public class Test implements A, B {
public void a() {}
public void b() {}
}
14
Print(); ColoredPrint(); Fax(); Scan();
Printer
Canon Professional Canon Office
Canon Basic
Print(){}
Print(){}
Print(){} Fax(){}
Scan(){}
ColoredPrint(){} Scan(){}
Fax(){}
Scan(){}
16
The java instanceof operator is used to test whether the
object is an instance of the specified type (class or subclass
or interface).
It returns either true or false. If we apply the instanceof
operator with any variable that has null value, it returns
false.
class Simple1{
public static void main(String args[]){
Simple1 s=new Simple1();
System.out.println(s instanceof Simple1);//true
}
}
The Hat is "loosely coupled" to the body. if you want to change the skin,
◦ This means you can easily take the hat off you would also HAVE TO change
without making any changes to the the design of your body as well
person/body. because the two are joined -
◦ When you can do that then you have "loose they are “tightly coupled”
coupling".
In short, loosely coupling makes code easier
to change.
loose coupling means that you need to know less
of the used code. Thus using interfaces provides
The Hat is "loosely coupled“ to the a part of that.
body.
18
Loose coupling is simply writing software in such a way that all
your classes can work independently without relying on each
other. MVC is a loosely coupled
• Model - Contains your classes and business logic
• View - Displays the application UI based on the model data
• Controller - Respond to an oncoming URL request and select the
appropriate view
19
Lab exercise
Write the classes and
interfaces given by
following diagram.
Override methods for
each class given by table
(symbols: #protected, ~
default -private, +
public).
Note: Use the table in the
next slide.
20
21
22