Pages

Monday, April 13, 2015

Abstraction, Abstract class, methods in Java example

Abstract class in Java:

Abstraction defines the ability to make a class as "abstract" declaring with keyword "abstract" which is considered as incomplete.

A class which is declared with the abstract keyword is known as an abstract class in Java. It can have abstract and non-abstract methods (method with the body).

abstract keyword is used to create a abstract class and method. Abstract class in java can't be instantiated. An abstract class is mostly used to provide a base for subclasses to extend and implement the abstract methods and override or use the implemented methods in abstract class


abstract_class


Rules or Conditions of Abstract Class in Java:


1) Once we declare a class "abstract" than object can not created or instantiated for the abstract class. if we try, this will give compile time error.
2) Abstract class can have fields(properties), abstract methods.
3) This can have non-abstract methods.
4) If we declare a method as abstract than no need to provide method body. Whoever is extending this class, they have to provide implementation for the abstract methods.
5) This does not have much importance unless it has sub classes.
6) Abstract methods should be public or protected.

7) Abstraction can be achieved either by declaring class as "abstract" or interfaces.
8) If one method is abstract in any class, that class must declared as "abstract".
9) If sub class does not provide implementation than that sub class must be declared as "abstract".
10) If we do not know the implementation for a method that will be implemented by its sub class, than declare it as abstract.
11) Way to hide implementation.

Example for abstract:

We have mineral water suppliers. We have created a class "WaterCan" declared as abstract and has methods named setDimensions as abstract and manfacPlace.

So whoever is implementing, that class should have implementation for abstract methods.


package com.java.w3schools.core.abstracts;

public abstract class WaterCan {
 public int quantity;
 public abstract void setDimensions(double height, double diameter);
 public String manfacPlace() {
  return "Bangalore";
 }
}


Bisleri brand extends WaterCan.. so need to implement for abstract method. because dimensions can be varied from brand to brand.


class Bisleri extends WaterCan {
 double h;
 double d;
 @Override
 public void setDimensions(double height, double diameter) {
  h = height*1.2;
  d = diameter;
 }
}

Similar to Himalaya brand.


class Himalaya extends WaterCan {
 double h;
 double d;
 @Override
 public void setDimensions(double height, double diameter) {
  h = height * 1.5;
  d = diameter * 0.1;
 }
}

package com.java.w3schools.core.abstracts;
public class BisleriMain {
 public static void main(String[] args) {
  Bisleri bisleri = new Bisleri();
  bisleri.setDimensions(12, 20);
  System.out.println(bisleri.manfacPlace());
 }
}

No comments:

Post a Comment

Please do not add any spam links in the comments section.