0% found this document useful (0 votes)
7 views9 pages

12.abstraction 1

The document explains abstraction in Java, detailing un-implemented and implemented methods, and how to achieve abstraction using abstract classes and interfaces. It highlights the differences between abstract classes and interfaces, including method types, access modifiers, and inheritance rules. Additionally, it discusses the evolution of interfaces from JDK 7 to JDK 9, emphasizing the importance of overriding methods in child classes.

Uploaded by

shitalkadam320
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

12.abstraction 1

The document explains abstraction in Java, detailing un-implemented and implemented methods, and how to achieve abstraction using abstract classes and interfaces. It highlights the differences between abstract classes and interfaces, including method types, access modifiers, and inheritance rules. Additionally, it discusses the evolution of interfaces from JDK 7 to JDK 9, emphasizing the importance of overriding methods in child classes.

Uploaded by

shitalkadam320
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Abstraction in Java

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.

 How to achieve abstraction in java:


1] Abstract class [0-100%]
2] Interface [100%]
---------------------------------------------------------------------------------------------------------------------------
1] Abstract class [0-100%]
[Students must perfect in inheritance and method Overriding]
A] Abstract class contains implemented or un-implemented or both type of methods.
Example:
abstract public class Test abstract public class Test
{ {

//un-implemented method public void method2()


abstract public void method1(); {
// implemented method
public void method2() }
{
// implemented method
}
}

// conclusion we can make any class as


} abstract class without knowing its content.

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.

Q- How to call or access abstract class functions?


-By using child class only. [Inheritance]
Q- If any class contains only implemented methods, can we make that class as abstract?
-Yes. Abstract class is with or without unimplemented method.

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 methodthen 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 interfaceprovide name of interface

Access-modifier interface InterfaceName


{

//Abstract method

//variable

Highlighted Points:
1] Interface methods are abstract only before JDK 7 version.

2] Un-implemented methods inside interface is always public abstract only


Example: public abstract void method1 ();
Since, it’s not compulsory to write public abstract before un-implemented method, java treated it as public abstract.

3] Variable inside interface is always public static final only


Example: public static final int a=45;
Since, it’s not compulsory to write public static final before variable, java treated it as public static final

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

Then, Test implements Xyz  [already Xyz extends Abc]

PerfectLearn-9168313434-[Offline/Online]
Key Points Between Abstractions

abstract Class interface


-It contains abstract or implemented methods. -Strictly all methods are abstract only still JDK 7.

-Then after JDK 8 we can take default and abstract

methods also.

-After JDK 9 we can go with private method also.

-We can go with any access modifier except private for -Access modifier is public only. Because, interface

abstract class methods only. methods are public only.

- In child class for overriding method follow overriding

rule.

-To access Abstract classby extends only. -To access interface by using implements only.

-Multiple inheritance is not permitted. -Multiple inheritance is permitted.

-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

abstract method to implemented method. abstract method to implemented method.

- 0 to 100 % abstraction can takes place. 100% abstraction takes place before JDK 7.

PerfectLearn-9168313434-[Offline/Online]

You might also like