Java 6 Absraction, Interface
Java 6 Absraction, Interface
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.
File: TestAbstraction1.java
drawing circle
17. }}
Rate of Interest is: 7 %
Rate of Interest is: 8 %
Rule: If you are extending an abstract class that has an abstract method, you must
either provide the implementation of the method or make this class abstract.
Note: If you are beginner to java, learn interface first and skip this example.
1. interface A{
2. void a();
3. void b();
4. void c();
5. void d();
6. }
7.
8. abstract class B implements A{
9. public void c(){System.out.println("I am c");}
10. }
11.
12. class M extends B{
13. public void a(){System.out.println("I am a");}
14. public void b(){System.out.println("I am b");}
15. public void d(){System.out.println("I am d");}
16. }
17.
18. class Test5{
19. public static void main(String args[]){
20. A a=new M();
21. a.a();
22. a.b();
23. a.c();
24. a.d();
25. }}
26.
Output:I am a
I am b
I am c
I am d
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.
Syntax:
1. interface <interface_name>{
2.
3. // declare constant fields
4. // declare methods that abstract
5. // by default.
6. }
7. interface printable{
8. void print();
9. }
10. class A6 implements printable{
11. public void print(){System.out.println("Hello");}
12.
13. public static void main(String args[]){
14. A6 obj = new A6();
15. obj.print();
16. }
17. }
Out put: Hello
Output:
drawing circle
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
A7 obj = new A7();
obj.print();
obj.show();
}
}
Output:Hello
Welcome
1. interface printable{
2. void print();
3. }
4. class A6 implements printable{
5. public void print(){System.out.println("Hello");}
6.
7. public static void main(String args[]){
8. A6 obj = new A6();
9. obj.print();
10. }
11. }
Output:
Hello
1. interface Printable{
2. void print();
3. }
4. interface Showable{
5. void print();
6. }
7.
8. class TestInterface3 implements Printable, Showable{
9. public void print(){System.out.println("Hello");}
10. public static void main(String args[]){
11. TestInterface3 obj = new TestInterface3();
12. obj.print();
13. }
14. }
Output:
Hello
Interface inheritance
A class implements an interface, but one interface extends
another interface.
1. interface Printable{
2. void print();
3. }
4. interface Showable extends Printable{
5. void show();
6. }
7. class TestInterface4 implements Showable{
8. public void print(){System.out.println("Hello");}
9. public void show(){System.out.println("Welcome");}
Output:
Hello
Welcome
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, protected, default.
etc.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
Output:
I am a
I am b
I am c
I am d