Q) What are the difference between
concrete class and abstract class?
Ans):-
1.
Create class are able to allow only concrete
methods.
Abstract classes are able to allow both
concrete method and abstract method.
2.
For concrete classes, we are able to create both
reference variable and objects.
For abstract classes, we are able to create
only reference variable and we are unable to create objects.
3.
To declare concrete classes, only class keyword
is sufficient.
To declare abstract classes, we have to use
“abstract” keyword.
4.
In java applications, concrete classes are able
to provide less sharability.
5.
Abstract classes are able to provide more
sharability.
1.
Interfaces :
Interface is a java keyword, it can be use to declare interfaces.
Interface is able to allow only abstract methods.
In java applications, for interfaces, we are able to create only
reference variables, we are unable to create objects.
In case of interfaces, by default, all the variable are “public static
final”, no need to declare explicitly.
In case of interfaces, by default, all the methods are “public and
abstract, no need to declare explicitly.
In java applications, interfaces are able to provide more sharability
when compared with classes and abstract
classes
Procedure to use interfaces in java applications:-
1.
Declare an interface with “interface” keyword
2.
Provide variable and methods inside the
interfaces.
3.
Declare an implementation class for the
interface by including “implements” keyword.
4.
Provide implementation for all the interface
method in the implementation class
5.
In main class, in main() method. Create object
for implementation class and declaration reference variable either for
interface or for implementation class.
6.
Access interface members.
Note :-
If we declare reference variable for interface then we are
able to access only interface members, but if we declare reference variable to
implementation class then we are able to access both interface members and
implementation class members.
Ex code :-
interface I
{
int
x=10;
void
m1();
void
m2();
void
m3();
}
class A implements I
{
public
void m1()
{
System.out.println("m1-A");
}
public
void m2()
{
System.out.println("m2-A");
}
public
void m3()
{
System.out.println("m3-A");
}
public
void m4()
{
System.out.println("m4-A");
}
}
class Test
{
public
static void main(String[] args)
{
//I
i=new I();---->Error
I
i=new A();
System.out.println(i.x);
System.out.println(I.x);
i.m1();
i.m2();
i.m3();
//i.m4();--->Error
A
a=new A();
System.out.println(a.x);
System.out.println(A.x);
a.m1();
a.m2();
a.m3();
a.m4();
}
}
Output:-
10
10
m1-A
m2-A
m3-A
10
10
m1-A
m2-A
m3-A
m4-A
No comments:
Post a Comment