Java Abstraction

Java Abstraction

It is a process of hiding implementation details and showing only functionality to the user.

In java we have two types of methods

i) Concrete methods (The methods which are having body)

Example:
public void add(){
Statements
---------
--------
---------
}

ii) Abstract methods (The methods which are not having body)

Ex:

public void add();
-------------------------------------------------
if we know the method name, but don't know the method functionality, then we go for abstract methods.

> Java Class contains 100% concrete methods

> Abstract Class contains one or more abstract methods.

Example:

Class 1 (having 10 methods)

(10 methods are concrete methods)

It is a Java Class
-------------------------
Class 2 (having 10 methods)

(5 concrete Methods and 5 abstract methods)

Abstract class
Class 3 (having 10 methods)

(10 methods are Abstract methods)

Abstract class
----------------------------------
public class HeroHonda extends Bike {

    @Override
    public void handle() {
        System.out.println("Bikes have handle");
        }
    @Override
    public void seat() {
        System.out.println("Bikes have Seat");
    }
    public static void main (String [] args){
        HeroHonda obj = new HeroHonda();
        obj.handle();
        obj.engine();
        obj.seat();
    }
-------------------------------------------
Interfaces in Java
> Interface is a Java type definition block which is 100% abstract.

> All the Interface methods are by default public and abstract.

> Static and final modifiers are not allowed for interface methods.

> In Interfaces variables have to initialize at the time of declaration.

Example:

int a; // Incorrect

int a = 10; // Correct

> In interfaces variables are public static final by default.

> Interface is going to be used using "implements" keyword.
--------------------------------------------
Example:
public class Example2 implements Example1{

    @Override
    public void add() {
        System.out.println("Addition");
        }

    @Override
    public void sub() {
        System.out.println("Subtraction");
        }
public static void main (String [] args){
    Example2 abc = new Example2();
    abc.add();
    abc.sub();
}
}
--------------------------------------------------------
From Class (Concrete Class or Abstract Class) to Class we use "extends" keyword

i) From Concrete Class to Sub class - we can access Super class methods directly.

ii) From Abstract Class to Sub class - Implement all abstract methods then we can access all methods.
----------------------------------------------
From Interface to Class we use "implements" keyword

From Interface to Class - Implement all methods and access.
------------------------------------------------------------------

0 comments:

Post a Comment