SE101_Lec4_InheritanceAbstractFinal
SE101_Lec4_InheritanceAbstractFinal
Interface
Multiple
Programming in
Inheritance
Java
OOP Concepts Revisited
Java Basics
Methods
DONE… Constructors
Encapsulation
Inheritance
Interface
Interface
Multiple Inheritance
Inheritance can be defined as the process where
one object acquires the properties of another.
}
public class TestDog{
15
Interface is a conceptual entity
similar to a Abstract class.
Can contain only constants (final
variables) and abstract method (no
implementation) - Different from
Interfaces Abstract classes.
Use when a number of classes share
a common interface.
Each class should implement the
interface.
16
Interface An interface is basically a kind of class, but
they have to be only abstract classes and final
s: An fields/variables.
informal
way of Therefore, it is the responsibility of the class
that implements an interface to supply the
realizing code for methods.
multiple
inheritan A class can implement any number of
ce interfaces, but cannot extend more than one
class at a time.
<<Interface>>
Speaker
Interface - speak()
Example
18
interface InterfaceName {
// Constant/Final Variable Declaration
Implement
class Priest implements Speaker {
ing public void speak(){
Interfaces }
System.out.println(“Religious Talks”);
Example }
24
Final and Abstract Classes
25
Parent
Restricting
Inheritance Inherited
capability
Child
26
Final
Members
All methods and variables can be
: A way overridden by default in subclasses.
for This can be prevented by declaring
Preventin them as final using the keyword “final”
g as a modifier. For example:
final int marks = 100;
Overridin final void display();
g of This ensures that functionality defined
Members in this method cannot be altered any.
Similarly, the value of a final variable
in cannot be altered.
Subclass
es 27
We can prevent an inheritance of classes by
other classes by declaring them as final
Final classes.
Classes: This is achieved in Java by using the keyword
final as follows:
A way for final class Marks
Preventin { // members
}
g Classes final class Student extends Person
being { // members
}
extended Any attempt to inherit these classes will
cause an error.
28
When we define a class to be “final”, it cannot be
extended.
In certain situation, we want to properties of
classes to be always extended and used. Such
classes are called Abstract Classes.
Abstract
Classes An Abstract class is a conceptual class.
An Abstract class cannot be instantiated – objects
cannot be created.
Syntax }
When a class contains one or more abstract methods,
it should be declared as abstract class.
The abstract methods of an abstract class must be
defined in its subclass.
We cannot declare abstract constructors or abstract
static methods.
30
Abstract Class - Shape is a abstract
Example class.
Shape
Circle Rectangle
31
public abstract class Shape {
public abstract double area();
public void move() { // non-abstract method
// implementation
The Shape }
Abstract }
Class
32
public Circle extends Shape {
protected double r;
protected static final double PI =3.1415926535;
public Circle() { r = 1.0; )
public double area() { return PI * r * r; }
Abstract …
}
Classes public Rectangle extends Shape {
protected double w, h;
public Rectangle() { w = 0.0; h=0.0; }
public double area() { return w * h; }
}
33
A class with one or more abstract
methods is automatically abstract and
it cannot be instantiated.
A class declared abstract, even with
no abstract methods can not be
Abstract instantiated.
Classes A subclass of an abstract class can be
Properties instantiated if it overrides all abstract
methods by implementation them.
A subclass that does not implement all
of the superclass abstract methods is
itself abstract; and it cannot be
instantiated.
34
If you do not want (properties of) your class
to be extended or inherited by other classes,
define it as a final class.
Java supports this is through the keyword “final”.
This is applied to classes.
You can also apply the final to only methods
if you do not want anyone to override them.
If you want your class (properties/methods)
Summary to be extended by all those who want to use,
then define it as an abstract class or define
one or more of its methods as abstract
methods.
Java supports this is through the keyword
“abstract”.
This is applied to methods only.
Subclasses should implement abstract methods;
otherwise, they cannot be instantiated.
35
NEXT CLASS:
Practical Session:
THANK
YOU