You are on page 1of 20

Abstract Class

Abstract class is class with atleast one Pure Virtual function.


virtual void f()=0;
Abstract class cannot be instantiated, but pointers and
refrences of Abstract class type can be created.
Abstract class can have normal functions and variables along
with a pure virtual function.
Classes inheriting an Abstract Class must implement all pure
virtual functions, or else they will become Abstract too.
E.g. on next slide.

#include<iostream>
class Base
{
public:
virtual void foo() =0;
};
class late: public Base
{ public:
void foo() { std::cout<<"Derived"; } //Needed, its mandetory
};
int main()
{
Base *b = new late();

b->foo();

b = new late1();

b->foo();

//Base obj; late.cpp:26:11: error: cannot declare variable obj to be of


abs type Base
//obj.foo();
}

Why cant we create object of an


abstract class?

When we create a pure virtual function in


Abstract class, we reserve a slot for a function
in the VTABLE, but doesn't put any address in
that slot. Hence the VTABLE will be incomplete.
As the VTABLE for Abstract class is incomplete,
hence the compiler will not let the creation of
object for such class and will display an errror
message whenever you try to do so.

Thats more confusing whats


VTable?

Virtual Function Table (vtable) created for any


class with virtual function(s)

One for base class, one for each derived class

Vtable entries point to methods for appropriate class

Constructor sets a pointer in each object to point


to appropriate vtable
Executing program indexes into vtable to select
appropriate function implementation for each
function call

Can You please give an Example


Antriksha Sir?

Antriksha Sir think for an minute and answers,


Ok Please see an example in next slide, and
next to it for some good example.

The next example is to make you understand

And another is to confuse you more :P.

Does the same things Happens for


JAVA Sir?

Why dont you find out? (Remember 20:80 rule)

Same rules as C++ except the


keyword Virtual
abstract class Canine extends Animal
{
public void roam() { }
}
public class MakeCanine
{
public void go()
{

Canine c;
//Its ok cause its an ref and be used by sub class
c = new Dog();
c = new Canine(); // Now this is serious trouble
c.roam();
}

Virtual Function

Its simply a function with no defination.

Why Virtual Function

Inheritable method implementations (in other words, methods with actual body) are
good thing to put in a superclass that makes sense but in the abstract class you
cannot use anything. So by using method in place of class you can come up with
some generic code.

Try a code of animals->Canine-> Wolf

Whats all this ???

Definitions

abstract methods = Methods that are declared,


with no implementation

abstract class = A class with abstract methods,


not meant to be instantiated

interface = A named collection of method


definitions (without implementations)

Examples

Food is an abstract class. Can you make an instance of


food? No, of course not. But you can make an instance of
an apple or a steak or a peanut butter cup, which are types
of food. Food is the abstract concept; it shouldnt exist.
Skills are interfaces. Can you make an instance of a
student, an athlete or a chef No, but you can make an
instance of a person, and have that person take on all
these skills. Deep down, its still a person, but this person
can also do other things, like study, sprint and cook.

So whats the difference(Inter vs


Class)

An interface cannot implement any


methods,whereas an abstract class can
A class can implement many interfaces but canhave
only one superclass (abstract or not)
An interface is not part of the class hierarchy.
Unrelated classes can implement the sameinterface
Can have multiple inheritance with the interface

public interface LoginAuth


{
public String encryptPassword(String pass);
public void checkDBforUser();
}
/*Now suppose you have 3 databases in your application. Then each and
every implementation for that database needs to define the above 2
methods: */
public class DBMySQL implements LoginAuth{
// Needs to implement both methods
}
public class DBOracle implements LoginAuth{
// Needs to implement both methods
}
public class DBAbc implements LoginAuth{
// Needs to implement both methods
}
But what if encryptPassword() is not database dependent, and it's the
same for each class? Then the above would not be a good approach.

Instead, consider this approach:


public abstract class LoginAuth{
public String encryptPassword(String pass){
// Implement the same default behavior here
// that is shared by all subclasses.
}
// Each subclass needs to provide their own implementation of
this only:
public abstract void checkDBforUser();
}
Now in each child class, we only need to implement one method
- the method that is database dependent.
I tried my best and Hope this will clear your doubts.

Syntex
Syntax:

abstract class:
public class Apple extends Food { }

interface:
public class Person implements Student,
Athlete, Chef { }

Experiments With Abstract Class

What are things possible with virutal class in


JAVA?

Can it have normal function?

Can it have data members?

Is it necessary to implement in the child class or it


can be made virtual too. (Use wolf example)

Experiments With Abstract Method

What are things possible with virutal method in


JAVA?

Can it have data members?

Is it necessary to implement in the child class or it


can be made virtual too.

Interface

Is it possible to have a data member in


Interface?
What will happen if we define any function in
virtual class?
Can multiple inheritance can be done with
interfaces.

You might also like