Skip to main content

JAVA-OOPS: ABSTRACTION

This post covers the topic Abstraction

 ABSTRACTION:

  • Hiding the implementation part is called abstraction
  • It has 2 types,
    1. Partial abstraction (abstract class)
    2. Full abstraction (interface)

1. Partial Abstraction (Abstract class):

  • It will support abstract method and non-abstract method.
  • We can’t create object for abstract class because in the method signature we didn't mention any business logic.
  • In abstract method, we only mention abstract signature, won't create business logic
  • It have 2 class, abstract class (sub class) and super class. we create object and business logic only in super class, won't create in abstract class

Example Program:

abstract class

public abstract class Bank {
    abstract void saving();        //method signature
    abstract void current();
    abstract void salary();
    abstract void joint();
    public void branchDetails(){
    System.out.println("chennai");
    }
}

super class

public class AxisBank extends Bank {
    public void saving() {         // method signature
        System.out.println("saving regular");         // business logic
    }
    public void current() {
        System.out.println("current");
    }
    public void salary() {
        System.out.println("salary");
    }
    public void joint() {
        System.out.println("joint");
    }
public static void main(String[] args) {
    AxisBank info = new AxisBank();
    info.branchDetails();
    info.salary();
    info.saving();
    }
}

Output:

chennai
salary
saving

2. INTERFACE / FULL ABSTRACTION;

  • It will support only abstract method, won't support non abstract method
  • In interface "public abstract" is default. we no need to mention
  • It is using implements keywords

Example Program:1

interface

public interface Bank {
    abstract void saving();
    abstract void current();
    abstract void salary();
    abstract void joint();
    public void branchDetails();
}

super class

public class AxisBank implements Bank {
    public void saving() {
        System.out.println("saving regular");
    }
    public void current() {
        System.out.println("current");
    }
    public void salary() {
    System.out.println("salary");
    }
    public void joint() {
    System.out.println("joint");
    }
    public void branchDetails() {
    System.out.println("chennai");
    }
public static void main(String[] args) {
    AxisBank info = new AxisBank();
    info.branchDetails();
    info.salary();
    info.saving();
    }
}

Output:

chennai
salary
saving regular
  • multiple inheritance its won’t support in java but using interface its support
  • here we have to create 2 interface(super class) and one sub class(normal). In the sub class we implement both interface

Example Program:2

interface

public interface ABank {
    public void test();
    }
    public interface HBank {
    public void test();
    }

sub class (normal class)

public class Bank implements ABank, HBank{
@Override
    public void test() {
    }
}
Abstract class Interface
Partial abstraction Full abstraction
It support both abstract method and non-abstract method It support only abstract method
It’s using "extends" keyword It’s using "implement" keyword
Here "public abstract" have to mention "public Abstract" is default. no need to mention
We can use whatever access specifier we want Here we use only public (access specifier)

Comments

Popular posts from this blog

Java Interview Questions For Selenium Testers

This post covers Core Java Interview Questions for Selenium Automation Testers. Question 1: Why did you choose Java? Answer:  Java is open source and easy to learn. Writing, Compiling and debugging is easy. We can reuse the code. Question  2: What are all the features of Java? Answer:  Question 3: Why Java is platform independent? Answer:  Java compiler converts Source code to Byte Code. We can run it in any platform such as Windows, Mac, and Linux etc. Question 4: Data types and Its Default Values? Answer: Data type has two types namely, Primitive : (Stores directly) Predefined Can store only one value There are no additional methods Non-primitive : (Stores based on reference) Not-Predefined Can store more than one value It references a memory location which stores the Data. (Reference variables) Default Values: Data Type Memory Size (Byte) Default Value Wrapper Class byte 1 0 Byte Short 2 0 Sh...