alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Java
    .net Framework
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

Java - Object-Oriented Programming (OOPS) Concept - Abstraction (Using abstract and interface) Tutorial

Abstraction is a process of showing only essential information to the user and hiding other internal detail (e.g function) which are not essential to show to the user.

For example.

Suppose a user is sending mail to another user and the user can only see the essential detail like mailto, message. but the user can’t see the function sending the mail to another user.

Abstract class and interface is been used in java to achieve abstraction.

 

  • Using Abstract Class

A Class that is declared with an abstract keyword is known as abstraction. We cannot achieve 100% abstraction using abstract keyword.

 

  1. Abstract Class should contain a method with the body or abstract method i.e abstract void fly();
  2. If a method inside the class is declared abstract, then class also has to be declared abstract.
  3. If a class is declared abstract. then it cannot be instantiated.
  4. To use abstraction, we have to inherit child class from the parents class.

Implementation detail is provided by the non-abstract class, which is hidden from the user.

 

abstract class Bird{ //Parent Class
abstract void fly(); //use void fly(){}
}
public class Parrot extends Bird{ //Child Class

void fly(){
System.out.print("Parrot can Fly ");
}

public static void main(String args[]){
Bird p=new Parrot();
p.fly();
}
}

Run Program

 

  • Using Interface

The interface in java is similar to a class. We can achieve 100% abstraction using Interface.

With interface multiple inheritances are possible.

An interface cannot be instantiated like an abstract class.

In the interface, we can have constants, nested types, default and static methods.

To declare interfaces we can use the interface keyword, in place of class.

 

Interface fields are public, static, and final by default. And the methods are public and abstract.

 

In the below example, the Bird interface has only one method named fly(), and its implementation is given by Parrot Class.

 

interface Bird{ //Parent Class
void fly();
}
public class Parrot implements Bird{ //Child Class
public void fly(){
System.out.print("Parrot can Fly ");
}

public static void main(String args[]){
Parrot p=new Parrot();
p.fly();
}
}

Run Program

 

Multiple inheritances

1] implements are used for inheritance between class and interface

2] extends are used for inheritance between two interfaces.

 

interface Bird1{ //Parent Class
void fly();
}

interface Bird2{//Parent Class
void eat();
}

public class Parrot implements Bird1,Bird2{ //Child Class

public void fly(){
System.out.println("Parrot can Fly ");
}

public void eat(){
System.out.println("Parrot can eat ");
}

public static void main(String args[]){
Parrot p=new Parrot();
p.fly();
p.eat();
}
}

Run Program

Java

Java

  • Introduction
  • Installation and running a first java program
    • JDK Installation
    • Environment Variable Path Setup
    • Running a first java program
  • 1st Java Program Explanation
    • Program Syntax
  • JDK,JRE,JVM
    • JRE
    • JDK
    • JVM
  • Thing to know before proceed
    • Variable
    • Datatype
    • Operator
    • Keyword
    • Access Modifier
  • Decision Control and looping statement
    • If-else
    • Switch
    • For
    • While
    • do-while
    • break
    • continue
  • Object-Oriented Programming (OOPS) Concept
    • About OOPs
    • Object & Class
    • Inheritance
    • Polymorphism (Runtime & Compile Time)
    • Abstraction (Using abstract and interface)
    • Encapsulation

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.