Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday, 20 March 2017

Java Abstract Class

 Abstract class in java

A class that is declared with the abstract keyword is known as an abstract class in java. Abstract class can have abstract and non-abstract methods.

First we understand, what is an abstraction in java then abstract class in java in detail.
java abstract class



Abstraction in java

If we say in simple world, abstraction means show functionality and hide implementation details(complexity) to the user. In other word , it shows only important thing or functions to the users and hide internal details to the user.

for example, one of the best example of abstraction is ATM machine. In ATM machine we can easily withdraw money, transfer money, etc. but we cannot know internal details of ATM, we can just use functions of ATM.

Another example of abstraction is sending massages, we just type the text and send the massage but we don't know the internal processing about the massage delivery.

Hiding of a data is known as data abstraction in java. Basically, the purpose of data abstraction is to provide security for the data from the unauthorized method.




How to achieve abstraction in java

There are two ways to achieve abstraction in java.
  1. Abstract class (achieve 0 to 100%)
  2. Interface (achieve 100%)

Abstract class in java

A class that is declared with "abstract" keyword is known as abstract class. Abstract class can have abstract method(without method body) and non-abstract method(with method body).It need  to be extended.

For example:

abstract class Employee
{
------
------
}




Abstract method in abstract class

A method that is declared with abstract keyword and end with semicolon and without method body is known as abstract method in java.

For example:

abstract class Employee
{
abstract void record();//abstract method
}


Example of java abstract class

In this example, there is abstract class Vehicle and contains abstract method run();. It implemetation provided by the Bike class.

abstract class Vehicle
{
abstract void run();//abstract method
}
class Bike extends Vehicle
{
void run()
{
System.out.println("bike is running");
}
public static void main(String args[])
{
Vehicle v = new Bike();
v.run();
}


output: bike is running


Some points about abstract class

  • Abstract class cannot be instantiated i.e you cannot create object of abstract class.
  • Only abstract class can have abstract method , you can't keep abstract method in normal or non-abstract class.
  • A child class has to override all the method of an abstract class otherwise it has to make itself as a abstract.
  • In case of abstraction a child class has to register itself with a parent class by passing its references to the parent class.


Constructor, Data Member, and Method in abstract class

An abstract class can have data members, constructor, abstract method, non-abstract method.

For example:

abstract class Vehicle 
{
int averagespeed = 40;
Vehicle()
{
System.out.println("Vehicle speed");
}
abstract void run();//abstract method without body
void type()//method with body
{
System.out.println("two wheeler");
}
}
class Bike extends Vehicle
{
void run()
{
System.out.println("Bike is running");
}
public static void main(String args[])
{
Vehicle v = new Bike();
v.run();
v.type();
System.out.println(v.averagespeed);
}
}

output: Vehicle speed
             Bike is running
             two wheeler
             40


Concrete class 

A concrete class is one which is containing method with method body or defined method or implemented method.

For example:

class ConcreteClass
{
void display()
{
System.out.println("hello java");
}
public static void main(String args[])
{
ConcreteClass c = new ConcreteClass();
c.display();
}


output: hello java

In the above example, there is a defined or implemented method in concrete class. We can create an object of concrete class.

Q. Can we keep a constructor in abstract class ?.

Ans. Yes.

Q. Can we declare abstract method as private, static, final ?.

Ans. No.

Q. Can we keep abstract method in non abstract class or normal class ?.

Ans. No.

Q. Can we keep normal method(with body) in abstract class ?.

Ans. Yes.

Read More:

Interview Questions on Abstraction in Java.
Interview Questions on Interface in Java.
Servlet Interview Questions.
Share:

6 comments:

  1. nice java blog.
    great thanks.

    ReplyDelete
  2. Adding more to this article, I would say that most IDE like Eclipse these day support full error reporting for wrong implementation of abstract class or interface. It is a good thing to use IDE for development. Some pros tend to use notepad but even then to err is human nature.

    ReplyDelete
  3. Great Article android based projects

    Java Training in Chennai Project Center in Chennai Java Training in Chennai projects for cse The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training Project Centers in Chennai

    ReplyDelete
  4. Hi, Thank you for this informative blog, I have just started to learn java course and this blog is really informative for me. Thank you for this blog!

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate