You are on page 1of 10

www.wscubetech.

com
info@wscubetech.com

Abstract class in Java

1st Floor, Laxmi Tower, Bhaskar Circle, Ratanada, Jodhpur.

Development – Training - Placement - Outsourcing


Abstraction

Abstraction is a process of hiding the implementation details


and showing only functionality to the user.

2
www.wscubetech.com
Ways to achieve Abstraction

There are two ways to achieve abstraction in java

a) Abstract class (0 to 100%)


b) Interface (100%)

3
www.wscubetech.com
Abstract class in Java

a) A class which is declared as abstract is known as an abstract class. It


can have abstract and non-abstract methods. It needs to be
extended and its method implemented.

b) An abstract class can not be instantiated, which means you are not
allowed to create an object of it.

4
www.wscubetech.com
Why we need an abstract class?

An abstract class allows you to create functionality that


subclasses can implement or override.

5
www.wscubetech.com
Points to Remember

a)An abstract class must be declared with an abstract


keyword.
b)It can have abstract and non-abstract methods.
c)It cannot be instantiated.
d)It can have constructors and static methods also.
e)It can have final methods which will force the subclass not
to change the body of the method.

6
www.wscubetech.com
abstract class

abstract class Animal {


//attributes and methods
}

If we try to create objects of an abstract class, we will get a compilation error.


For example,

Animal a1 = new Animal()


It will generate a compilation error: Animal is abstract; cannot be instantiated

7
www.wscubetech.com
abstract method

A method which is declared as abstract and does not have implementation is known as an abstract
method.
abstract void demo(); //no method body and abstract

It's important to note that, only an abstract class can contain abstract methods. If we include
abstract methods inside a class that is not abstract, we will get an error.

An abstract class can contain both abstract and non-abstract methods. Here's an example.

abstract class Animal {


public void demo1() //Non-abstract method
{ System.out.println(“I am an animal.”); }
abstract void demo2(); //Abstract Method
}

8
www.wscubetech.com
Understanding the real scenario of Abstract class

abstract class Bank{


abstract int getRateOfInterest();
}
class SBI extends Bank{
int getRateOfInterest(){return 8;}
}
class PNB extends Bank{
int getRateOfInterest(){return 9;}
}
class test{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %"); }}
9
www.wscubetech.com
www.wscubetech.com
info@wscubetech.com

Thank You
https://www.facebook.com/wscubetech.india https://www.linkedin.com/company/wscube-tech
https://plus.google.com/+wscubetechjodhpur https://www.youtube.com/c/wscubetechjodhpur

https://twitter.com/wscube

1st Floor, Laxmi Tower, Bhaskar Circle, Ratanada, Jodhpur.

Development – Training - Placement - Outsourcing

You might also like