Abstraction in java

How to achieve Abstraction in java?

  • Abstract class are used to achieve abstraction on java.
  • Abstraction is the concept of hiding the actual implementation from user.
  • Usually abstract method forces subclass to implement the methods.
  • By using abstract method we can have the consistency in method signature in all sub class.

Abstract Class in Java:

  • Abstract class consists of complete method or concrete method and abstract method.
  • The method with have method head and method signature is called concrete method or complete method.

Abstract Method:

  • The method without method body is called as abstract method.
  • This method also known as incomplete method because it’s contains only method headings.
Syntax:  abstract void test ();
Here test () is an abstract method.
  • Abstract method must ends with semicolon (;).
  • Abstract method should be declared with a keyword ‘abstract’ otherwise compiler throws error.
  • A class which has at least one abstract method is known as abstract class.
  • If a class has abstract methods, then it’s mandatory to declare that class as abstract, otherwise compiler through error.
  • For abstract class we can’t create object.
  • An abstract class can contains only concrete methods or only abstract methods or both concrete and abstract methods.
When to make a class as abstract?
  • When you have a requirement where your base class should provide default implementation of certain methods whereas other methods should be open to being overridden by child classes use abstract classes
  • The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.

 Sample Program :

public abstract class SampleProgram {

             abstract void test1 ();
             abstract void test2 ();
}
class SampleImplement extends SampleProgram{
   
void test1() {
 System.out.println("test1 in SampleImplement class");
}
void test2() {
 System.out.println("test2 in SampleImplement class");
 }
}
class Run{ 
 public static void main(String[] args) {
  SampleImplement sampleImplement = new SampleImplement();
  sampleImplement.test1();
  sampleImplement.test2();
  }
}

 Answer:
  • Yes, when we define a class to be an Abstract Class it cannot be instantiated but that does not mean an Abstract class cannot have a constructor. 
  • Each abstract class must have a concrete subclass which will implement the abstract methods of that abstract class. 
  • If we are not creating any constructor for abstract class, the java by default will create a constructor like other java class.
If we are in one of these below situation, we need to create constructor in an abstract class:
  • You have defined final fields in the abstract class but you did not initialize them in the declaration itself; in this case, you MUST have a constructor to initialize those fields
Sample Program:

package com.sample;

abstract class Motor { 
    int somenewProduct;
    public Motor( int somenewProduct ) {
        this.somenewProduct = somenewProduct;
    }
    public int MotorIncrese(int val) {
       return somenewProduct * val;
    }
}
class Honda extends Motor {
    public Honda() {
        super(2);
    }
}
class Cycle extends Motor {
    public Cycle(int cycleName) {
        super(cycleName);
    }
}








Copyright © 2017 qatoolsguide.blogspot.com || ALL RIGHTS RESERVED