Home > Java > javaTutorial > body text

Abstraction in Java

WBOY
Release: 2024-08-30 15:57:18
Original
974 people have browsed it

In java technology mainly built with the oops concepts Abstraction also one of the oops(object-oriented programming system) concepts is denoted as the process of hiding the datas that is it will not be shown some details but it will display with the user screen only for related information’s that is related to hided datas. The Abstraction in java we can use these concepts with the help of “abstract class and interfaces” these two types will be used in the java same time or else based on the user requirement we can use any of the types some times both processes will be used in the programs.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

The Abstraction will be used for two different types of syntax used in the java programs.

import packages;
abstract class names
{
-----some logic codes---
}
interface  names
{
----Some logic codes----
}
Copy after login

The above codes are basic syntax for applicable abstraction in java programs. When we use the abstract class it will use both the abstract and non-abstract methods but the interface accepts only non-abstract methods. They have some similar differences between these two concepts.

How to Use Abstraction in Java?

In Abstraction, it hides unwanted details in the java object it shows only relevant information about the subject it mostly compared to the java collection interface called map in map interface it has the derived classes like hashmap it will store the key values pairs in the user datas using some methods like get() and put() methods to store and retrieve the data from the backend.

Mainly abstraction provides two different ways 1.data abstraction and 2.control abstraction. In data abstraction is used to create the difficult types of datas it will be used to expose some useful contents to exposure in the screen including datatypes. In control abstraction the process of some datas to written in other programming languages is in different scenarios with multiple times to writes the codes for storing and retrieving the datas so these types of datas are to be identified and combined with a single piece of work is called as control abstraction.

While we use the interface types to achieve the abstraction means it has to be achieved these in most probably but in abstract classes is not fully achieved in the abstraction process it has to be partially complete. If we use abstract class we cannot create an instance of these classes if we want to use it for the instances in the completely or concrete these classes by extending it to them. Using Abstraction we declared methods implementation may be varied while we use new operator for creating an instance of the abstract class it will throw compilation error.

Examples of Abstraction in Java

Here are the following examples mention below :

Example #1

Code:

public abstract class Main {
private String names;
private String city;
public Main(String nm, String cty){
this.names=nm;
this.city=cty;
}
public abstract void work();
@Override
public String toString(){
return "Names="+this.names+"::City="+this.city;
}
public void changeName(String newNames) {
this.names = newNames;
}
}
public class ClientMain extends Main {
private int empId;
public ClientMain(String nm, String cty, int id) {
super(nm, cty);
this.empId=id;
}
@Override
public void work() {
if(empId == 0){
System.<em>out</em>.println("Invalid name");
}else{
System.<em>out</em>.println("Employees List!!");
}
}
public static void main(String args[]){
//coding in terms of abstract classes
Main m = new ClientMain("siva","tirupur",1);
Main m1 = new ClientMain("raman","chennai",234);
m.work();
m1.work();
//using method implemented in abstract class - inheritance
m1.changeName("Sivaraman");
System.<em>out</em>.println(m1.toString());
}
}
Copy after login

Output:

Abstraction in Java

Example #2

Code:

interface Animals {
public void animalSounds();
public void sleep();
}
class Sample implements Animals {
public void animalSounds() {
System.<em>out</em>.println("Animal Sounds");
}
public void sleep() {
System.<em>out</em>.println("Beep");
}
}
class ClientMain {
public static void main(String[] args) {
Sample s = new Sample();
s.animalSounds();
s.sleep();
}
}
Copy after login

Output:

Abstraction in Java

Example #3

Code:

@FunctionalInterface
interface SquareofNumbers
{
int numbers(int x);
}
class ClientMain
{
public static void main(String args[])
{
int a = 6;
SquareofNumbers  s = (int i)->i*i;
int result = s.numbers(a);
System.<em>out</em>.println(result);
}
}
Copy after login

Output:

Abstraction in Java

The above three examples we have used basic usages of abstract class and interfaces for implementing the abstraction in the java application the final example we have used the concept called functional interface used in java for squaring the given numbers.The functional interface same as the interface concept but in that it does not allowed more than one abstract methods in the program if suppose we used more than one abstract method means it will throws error like “unexpected @functional Interface annotation” while we used functional interface we also declare the annotation it is very much helpful for the data about the data concept that is while we need the metadata technology in java we have used these type of technology called annotation and also we have used another concept called lambda expressions we have used in the final example for the reduce the code space so the redundancy of the codes are very simple and the complexity of the programs are also less it is easily understandable for writing the complex codings.

If we defined any of the methods in the class then we should declare the class also abstract and also if we extend the abstract class and used in any of the methods in the inner classes before that we must implement all the methods of the abstract classes in java technology we are unable to create the instance of the abstract classes we can’t declare the abstract class as final access modifiers or keywords in java because when we use final in abstract class or any of the methods in the class it can’t be changed in the entire execution process of the applications. Private modifiers also not allowed in Abstraction it will affect the parent-child relationship inheritance should be affected in the Abstract classes because the functions/methods are not allowed for the sub classes.

Conclusion

The above concepts of the abstraction are basic understandings of the oops while we implement the same in java technology it should be used with the interfaces and abstract class for achieving the abstraction but in java latest versions it may vary according to the new features.

The above is the detailed content of Abstraction in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!