Abstraction in Java is a process of hiding the implementation details and showing only the functionality to the user. It can be achieved using abstract classes and interfaces.
An abstract class is a class that cannot be instantiated, but can be subclassed. It may contain abstract methods, which are methods without a body.
An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types.
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
void sound() {
System.out.println("Woof");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.sound();
}
}
Abstract classes can have both abstract methods and concrete methods.
Concrete subclasses must implement all abstract methods from the abstract class.
Console Output:
Woof
Interfaces specify what a class must do and are implemented by classes. They provide a form of multiple inheritance.
interface Animal {
void sound();
}
class Cat implements Animal {
public void sound() {
System.out.println("Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal myCat = new Cat();
myCat.sound();
}
}
Interfaces can be used to achieve loose coupling between classes.
A class can implement multiple interfaces, allowing for more flexible design.
Console Output:
Meow
Abstract methods are declared without an implementation and must be implemented in subclasses.
abstract class Vehicle {
abstract void run();
}
class Bike extends Vehicle {
void run() {
System.out.println("Bike is running");
}
}
public class Main {
public static void main(String[] args) {
Vehicle myBike = new Bike();
myBike.run();
}
}
Abstract methods provide a template for subclasses to follow.
All abstract methods in an abstract class must be implemented in its concrete subclasses.
Console Output:
Bike is running
Abstract classes can have constructors, member variables, and methods with implementations.
abstract class Shape {
String color;
Shape(String color) {
this.color = color;
}
abstract double area();
}
class Circle extends Shape {
double radius;
Circle(String color, double radius) {
super(color);
this.radius = radius;
}
double area() {
return Math.PI * radius * radius;
}
}
public class Main {
public static void main(String[] args) {
Shape myCircle = new Circle("Red", 5);
System.out.println("Area: " + myCircle.area());
}
}
Abstract classes can have state (fields) and behavior (methods).
Use abstract classes when you want to provide common base functionality to multiple classes.
Console Output:
Area: 78.53981633974483
Abstract classes can have method implementations, while interfaces cannot (until Java 8).
// Abstract class example
abstract class Animal {
abstract void eat();
}
// Interface example
interface LivingBeing {
void breathe();
}
class Human extends Animal implements LivingBeing {
void eat() {
System.out.println("Eating");
}
public void breathe() {
System.out.println("Breathing");
}
}
public class Main {
public static void main(String[] args) {
Human person = new Human();
person.eat();
person.breathe();
}
}
Interfaces provide a way to achieve multiple inheritance, while abstract classes allow for partial implementation.
Choose interfaces when you need to define a contract for other classes to follow.
Console Output:
Eating
Breathing
In a banking system, you can have an abstract class for Account with abstract methods like deposit() and withdraw().
abstract class Account {
abstract void deposit(double amount);
abstract void withdraw(double amount);
}
class SavingsAccount extends Account {
private double balance;
void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount);
}
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
} else {
System.out.println("Insufficient balance");
}
}
}
public class Main {
public static void main(String[] args) {
Account myAccount = new SavingsAccount();
myAccount.deposit(500);
myAccount.withdraw(100);
}
}
Abstraction allows you to focus on what the object does instead of how it does it.
Use abstraction to simplify complex systems by modeling classes based on their behavior.
Console Output:
Deposited: 500
Withdrawn: 100
Abstraction is a key principle in many design patterns such as Factory, Singleton, and Adapter.
interface Shape {
void draw();
}
class Rectangle implements Shape {
public void draw() {
System.out.println("Drawing Rectangle");
}
}
class Circle implements Shape {
public void draw() {
System.out.println("Drawing Circle");
}
}
public class Main {
public static void main(String[] args) {
Shape rectangle = new Rectangle();
Shape circle = new Circle();
rectangle.draw();
circle.draw();
}
}
Abstraction helps in creating a blueprint for objects, making software design more robust and scalable.
Use abstraction to define a common interface for a group of related classes.
Console Output:
Drawing Rectangle
Drawing Circle
In game development, abstraction is used to define game objects like Player, Enemy, and NPCs with common behaviors.
abstract class GameObject {
abstract void update();
}
class Player extends GameObject {
void update() {
System.out.println("Player is moving");
}
}
class Enemy extends GameObject {
void update() {
System.out.println("Enemy is attacking");
}
}
public class Main {
public static void main(String[] args) {
GameObject player = new Player();
GameObject enemy = new Enemy();
player.update();
enemy.update();
}
}
Abstraction helps in defining common behaviors for different game elements, making the code more manageable.
Use abstraction to define essential behaviors for game objects, allowing for easier updates and maintenance.
Console Output:
Player is moving
Enemy is attacking
Generics in Java provide a way to define classes, interfaces, and methods with a placeholder for types.
interface Container {
void add(T item);
T get();
}
class Box implements Container {
private T item;
public void add(T item) {
this.item = item;
}
public T get() {
return item;
}
}
public class Main {
public static void main(String[] args) {
Box stringBox = new Box<>();
stringBox.add("Hello");
System.out.println(stringBox.get());
}
}
Generics allow for type-safe code and reduce the need for casting.
Use generics to create flexible and reusable code components.
Console Output:
Hello
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies