xxxxxxxxxx
an abstract class can have abstract and non-abstract instance methods while an interface can have abstract or default instance methods;
an abstract class can extend another abstract or regular class and an interface can only extend another interface;
an abstract class can extend only one class while an interface can extend any number of interfaces;
an abstract class can have final, non-final, static, non-static variables (regular fields) while an interface can only have static final variables;
an abstract class can provide an implementation of an interface but an interface cannot provide an implementation of an abstract class;
an abstract class can have a constructor and an interface cannot;
in an abstract class, the keyword abstract is mandatory to declare a method as an abstract one while in an interface this keyword is optional.
xxxxxxxxxx
Interface
1) Interface contains only abstract methods
2) Access Specifiers for methods in interface
must be public
3) Variables defined must be public , static ,
final
4) Multiple Inheritance in java is implemented
using interface
5) To implement an interface we use
implements keyword
Abstract Class
1) Abstract class can contain abstract methods,
concrete methods or both
2) Except private we can have any access
specifier for methods in abstract class.
3) Except private variables can have any access
specifiers
4)We cannot achieve multiple inheritance using
abstract class.
5)To implement an interface we use implements
keyword
xxxxxxxxxx
Abstract class:
- Share code for multiple related classes
- Some implementations as shared code
- This class must be inherited (using 'extend' keyword)
Interface:
- Define a contract between multiple unrelated classes, no shared code
- No implementations at all
- It is a contract that must be implemented by those who accept the contract (using 'implements' keyword')
- It tells what to do, the class those implement it tells how to do it
xxxxxxxxxx
An abstract class can contain both abstract and non-abstract members
(methods, properties, fields, etc.), while an interface can only contain
abstract members.
A class can inherit from only one abstract class, but it can implement multiple
interfaces.
An abstract class can provide an implementation for some or all of its members,
while an interface cannot provide any implementation for its members.
An abstract class can define constructors and destructors, while an
interface cannot.
An abstract class can use access modifiers (public, private, etc.) to control
the visibility of its members, while an interface's members are always public
by default.
xxxxxxxxxx
Interfaces specify what a class must do and not how.
It is the blueprint of the class.
It is used to achieve total abstraction.
We are using implements keyword for interface.
Abstract=
Sometimes we may come across a situation
where we cannot provide implementation to
all the methods in a class. We want to leave the
implementation to a class that extends it.
In that case we declare a class
as abstract by using abstract keyword on method
signature.In my framework I have created my
PageBase class as super
class of the all page classes.
I have collected all common elements
and functions into PageBase class and
all other page classes extent PageBase class.
By doing so, I don't have to locate very
common WebElements and it provides
reusability in my framework.
Also
1)Abstract classes cannot be instantiated
2)An abstarct classes contains abstract method,
concrete methods or both.
3)Any class which extends abstarct class must
override all methods of abstract class
4)An abstarct class can contain either
0 or more abstract method.
xxxxxxxxxx
Interfaces specify what a class must do.
It is the blueprint of the class.
It is used to achieve total abstraction.
We are using implements keyword for interface.
Basic statement we all know in Selenium is
WebDriver driver = new FirefoxDriver();
WebDriver itself is an Interface.
So we are initializing Firefox browser
using Selenium WebDriver.
It means we are creating a reference variable
of the interface and creating an Object.
So WebDriver is an Interface and
FirefoxDriver is a class.
Sometimes we may come across a situation
where we cannot provide implementation to
all the methods in a class. We want to leave the
implementation to a class that extends it.
In that case we declare a class
as abstract by using abstract keyword on method
signature.In my framework I have created my
PageBase class as super
class of the all page classes.
I have collected all common elements
and functions into PageBase class and
all other page classes extent PageBase class.
By doing so, I don't have to locate very
common WebElements and it provides
reusability in my framework.
Also
1)Abstract classes cannot be instantiated
2)An abstarct classes contains abstract method,
concrete methods or both.
3)Any class which extends abstarct class must
override all methods of abstract class
4)An abstarct class can contain either
0 or more abstract method.
xxxxxxxxxx
Interfaces specify what a class must do.
It is the blueprint of the class.
It is used to achieve total abstraction.
We are using implements keyword for interface.
Basic statement we all know in Selenium is
WebDriver driver = new FirefoxDriver();
WebDriver itself is an Interface.
So we are initializing Firefox browser
using Selenium WebDriver.
It means we are creating a reference variable
of the interface and creating an Object.
So WebDriver is an Interface and
FirefoxDriver is a class.
Abstract Class
1) Abstract class can contain abstract methods,
concrete methods or both
2) Except private we can have any access
specifier for methods in abstract class.
3) Except private variables can have any access
specifiers
4)We cannot achieve multiple inheritance using
abstract class.
xxxxxxxxxx
--Difference between Abstract class and Interface-
Abstract class Interface
1. An Abstract class can have
abstract and non-abstract
methods.
An Interface can have only abstract methods. Since
Java 8, it can have default and static methods also.
2. An Abstract class doesn't
support multiple inheritance. .
An Interface supports multiple inheritance. .
3. An Abstract class can have
final, non-final, static and
non•static variables .
An Interface has only static and final variables .
4. An Abstract class can provide
the implementation of interface.
.
An Interface can't provide the implementation of
abstract class .
5. An abstract class can extend
another Java class and implement
multiple Java interfaces.
An interface can extend another Java interface only.
6. A Java abstract class can have
class members like private,
protected, etc.
Members of a Java interface are public by default.
xxxxxxxxxx
public class BusinessLogic : IBusinessLogic
{
public void Initialize()
{
println("hello");
}
}
xxxxxxxxxx
abstract class should be used primarily for objects that are closely related,
whereas interface is best suited for providing a common functionality to unrelated classes.
interface: If we are designing small, concise bits of functionality.
abstract class: If we are designing large functional units.