1. JAVA ESSENTIAL TRAINING
Abstract Class
Interface
Linh LeVan
lelinh2302@gmail.com
http://tinhocbk123.appspot.com/
1
2. &
VC
BB
Abstract Class
An abstract method is a method that is declared
without an implementation (without braces, and
followed by a semicolon), like this:
public abstract double getArea();
An abstract class is a class that is
declared abstract - it may or may not include
abstract methods.
Abstract classes cannot be instantiated, but they
can be sub-classed.
2
3. &
VC
BB
Abstract Class
If a class includes abstract methods,
the class itself must be declared abstract, as in:
public abstract class GraphicObject
{
//declare fields // declare non-abstract
methods abstract void draw();
}
3
4. &
VC
BB
Extending an Abstract Class
public abstract class LivingThing {
public void breath() {
System.out.println("Living Thing breathing...");
}
public void eat() {
System.out.println("Living Thing eating...");
}
public abstract void walk();
}
4
5. &
VC
BB
Extending an Abstract Class
When a concrete class extends the LivingThing
abstract class, it must implement the
abstract method walk(), or else, that subclass will
also become an abstract class, and therefore
cannot be instantiated. For example,
public class Human extends LivingThing {
public void walk() {
System.out.println("Human walks...");
}
}
5
6. &
VC
BB
Exercises
Create an abstract class Figure having variables
dim1,dim2 of double type and an abstract method
area, then make two subclasses Rectangle and
Triangle which will implement the area method.
Create the abstract class reference variable,
assign subclass objects to it and print the
corresponding area.
6
8. &
VC
BB
Multiple inheritance problem!
When we use the multiple inheritance, the child
class object can access all the methods of its
parent class.
Whenever we declare an object of child class
the system automatically loads all the methods
and variables of its superclass into that object.
But user generally does not work with all the
methods simultaneously.so why the object loads
all methods ,why not only required methods and
variables.
8
9. &
VC
BB
Interface solution
For this drawback multiple inheritance is not
included in java.so that we can declare only that
much thing we require.
Using interface, you can specify what a class
must do, but not how it does it.
9
10. &
VC
BB
Defining an Interface
An interface is defined much like a class:
access interface name{
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
// ...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
10
11. &
VC
BB
Meaning…
Here, access is either public or not used.
When no access specified is included, then
default access results, and the interface is only
available to other members of the package in
which it is declared.
When it is declared as public, the interface can
be used by any other code.
11
12. &
VC
BB
Meaning…
Name is the name of the interface, and can be
any valid identifier.
Notice that the methods which are declared
have no bodies. They end with a semicolon after
the parameter list.
They are, essentially, abstract methods; there
can be no default implementation of any method
specified within an interface.
Each class that includes an interface must
implement all of the methods. Variables can be
declared inside of interface declarations. 12
13. &
VC
BB
Meaning…
Each class that includes an interface must
implement all of the methods.
Variables can be declared inside of interface
declarations. They are implicitly final and static,
meaning they cannot be changed by the
implementing class. They must also be initialized
with a constant value.
All methods and variables are implicitly public if
the interface, itself, is declared as public.
13
14. &
VC
BB
An example of an interface definition
public interface IGreeting {
public static final int MAX_MEMBER = 20;
public void sayHello();
}
14
15. &
VC
BB
Implementing Interfaces
Once an interface has been defined, one or
more classes can implement that interface.
To implement an interface, include the
implements clause in a class definition, and then
create the methods defined by the interface.
access class classname [extends superclass]
[implements interface [,interface...]]{
// class-body
}
15
16. &
VC
BB
Example
public class EnglishGreeting implements
IGreeting{
@Override
public void sayHello() {
System.out.println("Hello Guy!");
}
}
Note :When you implement an interface method, it must be declared as public.
16
17. &
VC
BB
References to Interfaces
You can declare variables as object reference
that use an interface rather than a class type
When you call a method through one of these
references, the correct version will be called
based on the actual instance of the interface
being referred to. This one of the key features of
interface.
17
18. &
VC
BB
References to Interfaces (cont’d)
Demo:
public class TestInterface {
public static void main(String[] args) {
IGreeting javaInterfaceExample =
new EnglishGreeting();
javaInterfaceExample.sayHello();
}
}
18
19. &
VC
BB
Interface vs. Abstract Class
All methods of an Interface are abstract methods while
some methods of an Abstract class are abstract methods
Abstract methods of abstract class have abstract
modifier
An interface can only define constants while
abstract class can have fields
Interfaces have no direct inherited relationship with any
particular class, they are defined independently
Interfaces themselves have
inheritance relationship among themselves
If a class includes an interface but does not fully
implement the methods defined by that interface, then
that class must be declared as abstract. 19
20. &
VC
BB
Interfaces Can Be Extended
One interface can inherit another by use of the
keyword extends. The syntax is the same as for
inheriting classes.
When a class implements an interface that
inherits another interface, it must provide
implementations for all methods defined within
the interface inheritance chain.
20
21. &
VC
BB
Exercises
Define an interface having one method that takes
an integer parameter. For this method, provide two
implementations: In the first one,just print the value
and in the second one, print the square of
the number. Try to call both the versions.
21