4/8/19

Abstraction in java


Abstraction is a process of hiding the implementation details from the user. only the functionality will be provided to the user. In Java, abstraction is achieved using abstract classes and interfaces.

Abstraction is one of the four major concepts behind object-oriented programming (OOP). OOP questions are very common on job interviews, so you may expect questions about abstraction on your next Java job interview.

Introduction

An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types.

An abstract class is a class that is declared abstract. it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be sub classed. When an abstract class is sub classed, the subclass usually provides implementations for all of the abstract methods in its parent class.


Notes:
  • Interfaces have only abstract methods which have to implements by a class that is compulsory.
  • Abstract class have both abstract method and non-abstract method that abstract methods have to implements a class.
  • We can't create object both abstract classes and interfaces. only create a object for a Class.
  • Interface rather than abstract class has a advantage which has abstract method and non-abstract methods that can only implements the abstract method but you don't want to implements the non-abstract method, because non-abstract methods already have declared. 

Interface Example :

interface ICalc{
public int add(int x,int y);
public int sub(int x,int y);
}

class Calculator implements ICalc{
public int add(int x,int y){
return x+y;
}
public int sub(int x,int y){
return x-y;
}
}

public class Samle {

public static void main(String[] args){
Calculator calc = new Calculator();
System.out.println("Addition: "+calc.add(10,20));
System.out.println("Subtraction: "+calc.sub(50,40));
System.out.println();
}

}



Abstract Class Example:


abstract class SciClac{
abstract public int add(int x, int y);
abstract public int sub(int x,int y);
public int multi(int x, int y){
return x*y;
}
}

class SciCalculator extends SciClac{
public int add(int x, int y){
return x+y;
}
public int sub(int x,int y){
return x-y;
}
}

public class Samle {

public static void main(String[] args){
SciCalculator sc = new SciCalculator();
System.out.println("Addition: "+sc.add(10,20));
System.out.println("Subtraction: "+sc.sub(50,40));
System.out.println("Multiply: "+sc.multi(50,40));
}

}

No comments:

Post a Comment

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *