Post on 07-Apr-2018
8/4/2019 Abstract Interface
1/24
1
Final and Abstract Classes
8/4/2019 Abstract Interface
2/24
2
Uses of final keyword
To declare a named constant. To prevent overriding
To prevent inheritance
8/4/2019 Abstract Interface
3/24
3
na em ers: way or
Preventing Overriding ofMembers in Subclasses
All methods and variables can be overriddenby default in subclasses.
This can be prevented by declaring them as
final using the keyword final as a modifier.For example: final int marks = 100;
final void display();
This ensures that functionality defined in thismethod cannot be altered any. Similarly, thevalue of a final variable cannot be altered.
8/4/2019 Abstract Interface
4/24
4
na asses: way or
Preventing Classes beingextended
We can prevent an inheritance of classes by otherclasses by declaring them as final classes.
This is achieved in Java by using the keyword final asfollows:
final class Marks{ // members
}
final class Student extends Person
{ // members}
Any attempt to inherit these classes will cause anerror.
8/4/2019 Abstract Interface
5/24
5
Abstract Classes
When we define a class to be final, it cannotbe extended. In certain situation, we want toproperties of classes to be always extended andused. Such classes are called Abstract Classes.
An Abstractclass is a conceptual class. An Abstract class cannot be instantiated
objects cannot be created.
Abstract classes provides a common root for a
group of classes, nicely tied together in apackage:
8/4/2019 Abstract Interface
6/24
6
Abstract Class Syntax
abstract class ClassName{...abstract Type MethodName1();
Type Method2(){
// method body}
} 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.
8/4/2019 Abstract Interface
7/24
7
Abstract Class -Example Shape is a abstract class.
Shape
Circle Rectangle
8/4/2019 Abstract Interface
8/24
8
The Shape Abstract Class
Is the following statement valid? Shape s = new Shape();
No. It is illegal because the Shape class is an abstractclass, which cannot be instantiated to create its
objects.
public abstract class Shape {
public abstract double area();public void move() { // non-abstract method
// implementation
}
}
8/4/2019 Abstract Interface
9/24
9
Abstract Classespublic 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; }}
public Rectangle extends Shape {
protected double w, h;public Rectangle() { w = 0.0; h=0.0; }
public double area() { return w * h; }
}
8/4/2019 Abstract Interface
10/24
10
Abstract Classes Properties
A class with one or more abstract methods isautomatically abstract and it cannot beinstantiated.
A class declared abstract, even with no
abstract methods can not be instantiated. A subclass of an abstract class can be
instantiated if it overrides all abstractmethods by implementation them.
A subclass that does not implement all of thesuperclass abstract methods is itself abstract;and it cannot be instantiated.
8/4/2019 Abstract Interface
11/24
11
Summary
If you do not want (properties of) your class to beextended or inherited by other classes, define it as afinal class. Java supports this is through the keyword final. This is applied to classes.
You can also apply the final to only methods if youdo not want anyone to override them.
If you want your class (properties/methods) to beextended by all those who want to use, then define it
as an abstract class or define one or more of itsmethods 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.
8/4/2019 Abstract Interface
12/24
12
Interfaces
8/4/2019 Abstract Interface
13/24
13
Interfaces
Interface is a conceptual entity similar to aAbstract class.
Can contain only constants (final variables)
and abstract method (no implementation) -Different from Abstract classes.
Use when a number of classes share a
common interface. Each class should implement the interface.
8/4/2019 Abstract Interface
14/24
14
Interfaces: An informal way ofrealising multiple inheritance
An interface is basically a kind of classitcontains methods and variables, but they haveto be only abstract classes and final
fields/variables. Therefore, it is the responsibility of the class
that implements an interface to supply the codefor methods.
8/4/2019 Abstract Interface
15/24
15
A class can implement any number ofinterfaces, but cannot extend morethan one class at a time.
Therefore, interfaces are considered asan informal way of realising multiple
inheritance in Java.
8/4/2019 Abstract Interface
16/24
16
Interface - Example
speak()
Politician Priest
Speaker
speak() speak()
Lecturer
speak()
8/4/2019 Abstract Interface
17/24
17
Interfaces Definition
Syntax (appears like abstract class):
Example:
interface InterfaceName {
// Constant/Final Variable Declaration
// Methods Declaration only method body}
interface Speaker {
public void speak( );
}
8/4/2019 Abstract Interface
18/24
18
Implementing Interfaces
Interfaces are used like super-classeswho properties are inherited by classes.This is achieved by creating a class that
implements the given interface asfollows:
class ClassName implements InterfaceName [, InterfaceName2, ]{
// Body of Class
}
8/4/2019 Abstract Interface
19/24
19
Implementing InterfacesExample
class Politician implements Speaker {
public void speak(){
System.out.println(Talk politics);}
}
class Priest implements Speaker {public void speak(){
System.out.println(Religious Talks);}
}
class Lecturer implements Speaker {
public void speak(){
System.out.println(Talks Object Oriented Design and Programming!);}
}
8/4/2019 Abstract Interface
20/24
20
Extending Interfaces
Like classes, interfaces can also be extended.The new sub-interface will inherit all themembers of the superinterface in the manner
similar to classes. This is achieved by usingthe keyword extends as follows:
interface InterfaceName2 extends InterfaceName1
{
// Body of InterfaceName2
}
8/4/2019 Abstract Interface
21/24
21
Inheritance and InterfaceImplementation
A general form of interface implementation:
This shows a class can extended another class while
implementing one or more interfaces. It appears likea multiple inheritance (if we consider interfaces asspecial kind of classes with certain restrictions orspecial features).
class ClassName extends SuperClass implements InterfaceName [,
InterfaceName2, ]{
// Body of Class
}
8/4/2019 Abstract Interface
22/24
22
Student Assessment Example
Consider a university where students who participatein the national games or Olympics are given somegrace marks. Therefore, the final marks awarded =Exam_Marks + Sports_Grace_Marks. A class diagram
representing this scenario is as follow:Student Sports
Exam
Results
extends
extendsimplements
8/4/2019 Abstract Interface
23/24
23
Software Implementation
class Student
{// student no and access methods
}interface Sport{
// sports grace marks (say 5 marks) and abstract methods}class Exam extends Student{
// example marks (test1 and test 2 marks) and access methods}
class Results extends Exam implements Sport{
// implementation of abstract methods of Sport interface// other methods to compute total marks = test1+test2+sports_grace_marks;// other display or final results access methods
}
8/4/2019 Abstract Interface
24/24
24
Interfaces and SoftwareEngineering
Interfaces, like abstract classes and methods, providetemplates of behaviour that other classes areexpected to implement.
Separates out a design hierarchy from
implementation hierarchy. This allows softwaredesigners to enforce/pass common/standard syntaxfor programmers implementing different classes.
Pass method descriptions, not implementation
Java allows for inheritance from only a singlesuperclass. Interfacesallow for class mixing.
Classes implementinterfaces.