0% found this document useful (0 votes)
28 views

ABstract Class

Web Technology

Uploaded by

ayush2003kanpur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

ABstract Class

Web Technology

Uploaded by

ayush2003kanpur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Abstract Class

Abstract class
• A class that is declared with abstract keyword,
is known as abstract class in java. It can have
abstract and non-abstract methods (method
with body).
• An abstract class is a placeholder in a class
hierarchy that represents a generic concept.

Vehicle

Car Boat Plane


Abstraction in Java
• Abstraction is a process of hiding the
implementation details and showing only
functionality to the user.
• Another way, it shows only important things to
the user and hides the internal details.
• Abstraction lets you focus on what the object
does instead of how it does ?
Ways to achieve Abstaction:
There are two ways to achieve abstraction in java
• Abstract class (0 to 100%)
• Interface (100%)
Abstract Class: Example
 An abstract class often contains abstract methods, though it
doesn’t have to
 Abstract methods consist of only methods
declarations, without any method body
public abstract class Vehicle
{
String name;

public String getName()


{ return name; } \\ method body

abstract public void move();


\\ no body!
}
Abstract Classes
• An abstract class often contains abstract methods, though it
doesn’t have to
– Abstract methods consist of only methods declarations, without any
method body

• The non-abstract child of an abstract class must override the


abstract methods of the parent
• An abstract class cannot be instantiated
(why?)

• The use of abstract classes is a design decision; it helps us


establish common elements in a class that is too general to
instantiate
Abstract Classes
• You can extend (subclass) an abstract class
– If the subclass defines all the inherited abstract
methods, it is “complete” and can be instantiated
– If the subclass does not define all the inherited
abstract methods, it too must be abstract
• You can declare a class to be abstract even if
it does not contain any abstract methods
– This prevents the class from being instantiated
Why have abstract classes?
• Suppose you wanted to create a class Shape,
with subclasses Oval, Rectangle, Triangle,
Hexagon, etc.
• You don’t want to allow creation of a “Shape”
– Only particular shapes make sense, not generic ones
– If Shape is abstract, you can’t create a new Shape
– You can create a new Oval, a new Rectangle, etc.
• Abstract classes are good for defining a general
category containing specific, “concrete” classes
Why have abstract methods?
• Suppose you have a class Shape, but it isn’t abstract
– Shape should not have a draw() method
– Each subclass of Shape should have a draw() method
• Now suppose you have a variable Shape figure; where figure contains
some subclass object (such as a Star)
– It is a syntax error to say figure.draw(), because the Java compiler can’t tell in
advance what kind of value will be in the figure variable
– A class “knows” its superclass, but doesn’t know its subclasses
– An object knows its class, but a class doesn’t know its objects
• Solution: Give Shape an abstract method draw()
– Now the class Shape is abstract, so it can’t be instantiated
– The figure variable cannot contain a (generic) Shape, because it is impossible
to create one
– Any object (such as a Star object) that is a (kind of) Shape will have the
draw() method
– The Java compiler can depend on figure.draw() being a legal call and does
not give a syntax error
A problem
• class Shape { ... }
• class Star extends Shape {
void draw() { ... }
...
}
• class Crescent extends Shape {
void draw() { ... }
...
}
• Shape someShape = new Star();
– This is legal, because a Star is a Shape
• someShape.draw();
– This is a syntax error, because some Shape might not have a draw() method
– Remember: A class knows its superclass, but not its subclasses
A solution
• abstract class Shape {
abstract void draw();
}
• class Star extends Shape {
void draw() { ... }
...
}
• class Crescent extends Shape {
void draw() { ... }
...
}
• Shape someShape = new Star();
– This is legal, because a Star is a Shape
– However, Shape someShape = new Shape(); is no longer legal
• someShape.draw();
– This is legal, because every actual instance must have a draw() method
Another example of abstract class
abstract class Bank{
abstract int getRateOfInterest();
}

class SBI extends Bank{


int getRateOfInterest(){return 7;}
}
class PNB extends Bank{
int getRateOfInterest(){return 7;}
}

class TestBank{
public static void main(String args[]){
Bank b=new SBI();//if object is PNB, method of PNB will be invoked
int interest=b.getRateOfInterest();
System.out.println("Rate of Interest is: "+interest+" %");
}
}
Abstract Class with Constructor
Abstract class having constructor
• An abstract class can have
– Data Member
– Constructor
– Non Abstract Methods
– Abstract Methods
Example
abstract class Bike{
Bike(){
System.out.println("bike is created");
}
abstract void run();
void changeGear(){
System.out.println("gear changed");
}
}
class Honda extends Bike{
void run(){System.out.println("running safely..");
}
}
class TestAbstraction2{
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.changeGear();
}
}
Output :
bike is created
running safely..
gear changed
Rules
• If there is any abstract method in a class, that
class must be abstract.

• When object of concrete sub class is created


then it calls the parent class default constructor
up in inheritance hierarchy.

• If you are extending any abstract class that have


abstract method, you must either provide the
implementation of the method or make this
class abstract.
Example-2 using super
abstract class Product {
int multiplyBy;
public Product( int multiplyBy ) {
this.multiplyBy = multiplyBy;
}
public int mutiply(int val) {
return multiplyBy * val;
}
}
class TimesTwo extends Product {
public TimesTwo() {
super(2);
}
}
class TimesWhat extends Product {
public TimesWhat(int what) {
super(what);
}
}
Explanation of Example-2
• The superclass Product is abstract and has a
constructor.
• The concrete class TimesTwo has a constructor
that just hardcodes the value 2.
• The concrete class TimesWhat has a constructor
that allows the caller to specify the value.

NOTE: As there is no default (or no-arg) constructor


in the parent abstract class the constructor used in
subclasses must be specified.
Scenerio
You would define a constructor in an abstract class
if you are in one of these situations:

• you want to perform some initialization (to fields


of the abstract class) before the instantiation of a
subclass actually takes place.

• 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 these fields

You might also like