Abstraction In Java Example Code
Real time example of abstraction in java.
Abstraction in java example code. Abstraction can be achieved with either abstract classes or interfaces which you will learn more about in the next chapter. You can think of them as a class version of interfaces or as an interface with actual code attached to the methods. They can be subclassed or extended but cannot be instantiated. 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 can achieve abstraction in two ways. Abstraction in java data abstraction is the property by virtue of which only the essential details are displayed to the userthe trivial or the non essentials units are not displayed to the user. Abstraction in java abstraction is a process of hiding the implementation details and showing only functionality to the user. Abstract classes are classes declared with abstract.
Let the subclasses extend employee class and implement a calculatesalary method. Both subclasses have common properties to share like the name of the employee and the the amount of money the person will be paid per hour. So here you dont know the internal processing of how a call is generated thats the beauty of abstraction. For example when you consider the case of e mail complex details such.
In simplest words you can define abstraction as which captures only those details about a java object that are relevant to the current perspective. Suppose we have sport as an interface. Abstract classes in java explained with examples. Java abstract classes and methods.
But in reality there is a lot of code that runs in the background. A car is viewed as a car rather than its individual components. An example of abstract class is abstractmap which is part of collection framework include treemap hashmap and concurrenthashmap and share many methods like isempty put get containvalue containkey etc that abstractmap defines. Java abstraction as per dictionary abstraction is the quality of dealing with ideas rather than events.
It provides you two methods get and put methods to store and retrieve key value pairs from map. The abstract keyword is a non access modifier used for classes and methods. Employee contractor and fulltimeemployee example in this example we create an abstract employee class and which contains abstract calculatesalary method. Last updated on june 22 2020.
To give an example of abstraction we will create one superclass called employee and two subclasses contractor and fulltimeemployee. For example say you have a class vehicle which defines the basic functionality methods and components object variables that vehicles have in common.