12.abstraction 1
12.abstraction 1
Methods:
1] Un-implemented method:
The method is without method body or implementation is known as un-
implemented method.
Example: un-implemented method is always abstract.
Un-Implemented-Un-parameterized method Un-Implemented-Parameterized method
abstract public void methodName ( ); abstract public void methodName ( int a, byte b);
2] Implemented method:
The method is with method body or implementation is known as un- implemented
method.
Example:
Implemented-Un-parameterized method Implemented-Parameterized method
public void methodName ( ) public void methodName ( int a, boolean b)
{ {
// implementation or code or logic // implementation or code or logic
} }
Note:
1] In abstraction in child class we need to convert un-implemented method into implemented
method.
2] Converting un-implemented method into implemented method means we are overriding that
method.
PerfectLearn-9168313434-[Offline/Online]
What is Abstraction?
Hiding the implementation details from the users and only functionality will be provided to users.
User only use its functionality but user don’t know its implementation.
Ex-1: Zoom meeting-we are only using zoom meeting functionality, we don’t know about its internal
process.
Ex-2: email-compose mail-we are send mail by compose mail functionality, but how its work we are
unclear about it.
PerfectLearn-9168313434-[Offline/Online]
B] If any method from class is abstract [un-implemented] then that class must be abstract.
C] We can’t create object for abstract class.
Note:
1] Constructor never inherited so, no overriding and no abstraction.
2] If abstract class contains un-implemented methods then in child class we need to implements that
methods.
PerfectLearn-9168313434-[Offline/Online]
3] Suppose, in first child class we don’t need to implements that un-implemented methodthen make
that class as abstract then make another child class and implement them.
PerfectLearn-9168313434-[Offline/Online]
2] Interface [100%]
-Interface are similar to abstract class but having all methods are abstract type.
Use
1. To achieve abstraction.
2. It support multiple inheritance.
Syntax:-
How to create interface. New interfaceprovide name of interface
//Abstract method
//variable
Highlighted Points:
1] Interface methods are abstract only before JDK 7 version.
PerfectLearn-9168313434-[Offline/Online]
Since,
final permanent or Constance variable.
static single time memory allocation inside RAM.
4] But after JDK 8 we can go with implemented method also, but methods always static or default only
public static void method2()
{
// implemented
}
OR
public default void method2()
{
// implemented
}
5] After JDK 9 we can go with private method also.
Q- How to call or access interface in java?
- By using implements keyword only.
- Below image shows Class Test implements Abc (interface) So, first we need to convert all un-implemented
methods into implemented methods inside class Test.
Example-1
PerfectLearn-9168313434-[Offline/Online]
Example-2
- First we need to convert all un-implemented methods into implemented methods inside class Test and then we call
access or call it.
- Converting un-implemented method into implemented method means we are overriding that
method.
PerfectLearn-9168313434-[Offline/Online]
NOTE:
1] One interface can extends other interface also
PerfectLearn-9168313434-[Offline/Online]
Key Points Between Abstractions
methods also.
-We can go with any access modifier except private for -Access modifier is public only. Because, interface
rule.
-To access Abstract classby extends only. -To access interface by using implements only.
-Return Type We can go with any return type. Follow -Return Type We can go with any return type. Follow
method overriding rule for return type. method overriding rule for return type.
-We need to follow method overriding for converting -We need to follow method overriding for converting
- 0 to 100 % abstraction can takes place. 100% abstraction takes place before JDK 7.
PerfectLearn-9168313434-[Offline/Online]