Abstract &Interface Class 9
Abstract &Interface Class 9
Encapsulation in Java:
Encapsulation in Java is a process of wrapping code and data together into
a single unit, for example, a capsule which is mixed of several medicines.
We can create a fully encapsulated class in Java by making all the data
members of the class private. Now we can use setter and getter methods to
set and get the data in it.
It provides you the control over the data. Suppose you want to set the
value of id which should be greater than 100 only, you can write the logic
inside the setter method. You can write the logic not to store the negative
numbers in the setter methods.
It is a way to achieve data hiding in Java because other class will not be
able to access the data through the private data members.
The encapsulate class is easy to test. So, it is better for unit testing.
The standard IDE's are providing the facility to generate the getters and
setters. So, it is easy and fast to create an encapsulated class in Java.
File: Account.java
Abstraction and Interface
32.}
33.
34.}
File: TestAccount.java
Output:
Before learning the Java abstract class, let's understand the abstraction in
Java first.
Abstraction in Java
Abstraction is a process of hiding the implementation details and showing
only functionality to the user.
Another way, it shows only essential things to the user and hides the internal
details, for example, sending SMS where you type the text and send the
message. You don't know the internal processing about the message
delivery.
Abstraction lets you focus on what the object does instead of how it does it.
Points to Remember
o An abstract class must be declared with an abstract keyword.
o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.
o It can have final methods which will force the subclass not to change the
body of the method.
Abstraction and Interface
void demo(){
3. System.out.println("running safely");
}
4. class Honda4 extends Bike{
5. void run(){
6. System.out.println("running safely");
7. }
8. public static void main(String args[]){
9. Honda4 obj = new Honda4();
10. obj.run();
obj.demo();
11.}
12.}
Test it Now
running safely
Abstraction and Interface
In this example, if you create the instance of Rectangle class, draw() method
of Rectangle class will be invoked.
File: TestAbstraction1.java
File: TestAbstraction2.java
Interface in Java
An interface in Java is a blueprint of a class. It has static constants and
abstract methods.
In other words, you can say that interfaces can have abstract methods and
variables. It cannot have a method body.
1. interface printable{
2. void print();
3. int id;
4. }
Abstraction and Interface
Output:
Hello
File: TestInterface1.java
15.Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDr
awable()
16.d.draw();
17.}}
Test it Now
drawing circle
File: TestInterface2.java
1. interface Bank{
2. float rateOfInterest();
3. }
4. class SBI implements Bank{
5. public float rateOfInterest(){return 9.15f;}
6. }
7. class PNB implements Bank{
8. public float rateOfInterest(){return 9.7f;}
9. }
10.class TestInterface2{
11.public static void main(String[] args){
12.Bank b=new SBI();
13.System.out.println("ROI: "+b.rateOfInterest());
14.}}
Test it Now
Output:
ROI: 9.15
Abstraction and Interface
But there are many differences between abstract class and interface that are
given below.
3) Abstract class can have final, Interface has only static and final
non-final, static and non-static variables.
variables.
8) A Java abstract class can have Members of a Java interface are public by
class members like private, default.
protected, etc.
Abstraction and Interface
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
Notes on Interfaces:
Like abstract classes, interfaces cannot be used to create objects (in
the example above, it is not possible to create an "Animal" object in the
MyMainClass)
Interface methods do not have a body - the body is provided by the
"implement" class
On implementation of an interface, you must override all of its methods
Interface methods are by default abstract and public
Interface attributes are by default public, static and final
An interface cannot contain a constructor (as it cannot be used to create
objects)
2) Java does not support "multiple inheritance" (a class can only inherit from one
superclass). However, it can be achieved with interfaces, because the class
can implement multiple interfaces. Note: To implement multiple interfaces,
separate them with a comma (see example below).
Multiple Interfaces
To implement multiple interfaces, separate them with a comma:
Example
interface FirstInterface {
interface SecondInterface {
System.out.println("Some text..");
class Main {
myObj.myMethod();