Today i will be sharing my knowledge on the topics mentioned in title of this post, feel free to comment.
Interface:
Let me start with an example,
A child is given a homework...write lesson 1.1 .. learn lesson 1.2 ...draw a box etc...
Now the child has to IMPLEMENT this homework..
Not doing any of the homework is considered a fault.
The child has the freedom to implement the homework in its own way. for example he can write the lesson in pen or pencil, draw an ugly box or a neat one and color it...
Now comes the analogy...
The homework specifications is the interface...
What the child does and brings the next day to school is the class implementing that interface !
Sounds cool right?
Coming back to our technical world... now i tell you what’s an interface,
What’s an interface:
Interfaces are similar to classes with the following differences,
Uses of interface:
The above code throws error stating that calling function disp is ambiguous !! To remove this unnecessary complexity, Java removed multiple inheritance and brought in the concept of interface
I think that’s enough points said about interfaces... lets move on to next topic.
Abstract class:
When you need a class to act as base class only and you are sure that no object of that class is going to be created (or needed!) then you can declare that class as abstract class by using the modifier abstract.
An abstract class can in turn contain abstract methods or non-abstract methods. Abstract methods are essentially virtual , that is their overridden methods are run every time!
So you must be thinking what’s the difference between abstract classes and interfaces...
For Example, document can be an abstract class... passport, mark sheet etc can be the sub classes
Use:
Am going to tell only one use which i think is the most important use of abstract class. Almost every time a need for abstract class arises because of only this use,
It gives a protocol or rule like thing which every subclass should follow.
You should be clear with abstract classes now... let’s quickly move on to the final topic of the day.
Partial class:
As the name suggest a partial class is a type of class in which you have only partial implementation in one source file. Many such partial implementations of the same class can be present in different files. When running the application these partial implementations will be combined together by the compiler and seen as a single class !
In simple words they allow you to split a single class across files.
There are some general rules regarding these partial classes which i think you must know before you start using it...
Uses of splitting the class is limited only to the imagination of the developers.
Common use is that it supports parallel development...
For example, we have a bird class code to make it fly is being written by a person and code to make it eat is written by someone else.. then partial classes can be used and development can be done parallelly. Of course that’s one simple example, it could be inefficient in many ways.
In real time, Visual studio uses partial class to separate auto generated code from user code. (same class is required in both these codes ! so partial class fitted perfectly in the scheme of things)
Difference between a structure and a class:
This is probably the first question everyone would be having in their mind after learning about classes. The answer is not straightforward as you expect. It’s a language specific question. In different languages we have different methods of implementation of classes and structures.
In C:
In C we don’t have classes. I will brief about structures now. Structures were introduced to group related data. Structures are like user defined variables. They cannot have functions or methods inside them. You cannot have access specifiers inside it and by default everything inside a structure can be accessed anywhere out.
In C++:
In C++ they extended structure in such a way that it could now have member functions within it.
In C#:
Actually there are lot of differences between structure and class in C#. I will be mentioning a few below,
Example:
Consider you have a structure called Hello and you have a class called World...
Now ,
Hello h; // this will create a object for the structure Hello
World w; //this will only create a reference(a pointer like thing)
w=new World(); //only now you will be allocated an object and w is a reference to that object
i guess things are clearer now.
So if you do copy operations the following happen...
Hello hcpy=h; //hcpy will be a duplicate object of h
World wcpy=w; // wcpy is just a copy of the reference w. so now wcpy and w will be pointing to the same object of World !!
Other few minor differences are,
I hope you now have more than a basic idea about interfaces, abstract classes,partial classes and difference between structure and class.
See you in next post!
Trained @ Sourcebits
Interface:
Let me start with an example,
A child is given a homework...write lesson 1.1 .. learn lesson 1.2 ...draw a box etc...
Now the child has to IMPLEMENT this homework..
Not doing any of the homework is considered a fault.
The child has the freedom to implement the homework in its own way. for example he can write the lesson in pen or pencil, draw an ugly box or a neat one and color it...
Now comes the analogy...
The homework specifications is the interface...
What the child does and brings the next day to school is the class implementing that interface !
Sounds cool right?
Coming back to our technical world... now i tell you what’s an interface,
What’s an interface:
Interfaces are similar to classes with the following differences,
- it has to be declared with the keyword interface
- it cannot have data members or attributes( except static ones)
- it can have only method prototypes without implementation
- it cannot be inherited... but only be implemented
Uses of interface:
- Interfaces help those working with it to work without worrying about the implementation. For example a punctured car can be repaired by changing its punctured wheel with the stepney , since stepney has the same interface as the punctured wheel !! hope u get the point !
- It makes the system standardized ( all car wheels of a particular model are having same functionalities because of this point!)
- Encourages parallel development. Those who depend on a particular module can work independently if they are given the design of the interface which will be implemented by some other team.
Why multiple inheritance available in C is not supported in Java and Java uses interface instead?
#include<iostream>
using namespace std;
class A
{
public:
void disp()
{
cout<<"A";
}
};
class B
{
public:
void disp()
{
cout<<"B";
}
};
class C:public A,B
{
};
int main()
{
cout<<"ok";
C *c=new C();
c->disp();
return 0;
}
The above code throws error stating that calling function disp is ambiguous !! To remove this unnecessary complexity, Java removed multiple inheritance and brought in the concept of interface
Abstract class:
When you need a class to act as base class only and you are sure that no object of that class is going to be created (or needed!) then you can declare that class as abstract class by using the modifier abstract.
An abstract class can in turn contain abstract methods or non-abstract methods. Abstract methods are essentially virtual , that is their overridden methods are run every time!
So you must be thinking what’s the difference between abstract classes and interfaces...
- Theoretically speaking abstract classes can have members that are not static
- An abstract class can have protected member functions unlike interface where everything is public
- One important difference... and probably one reason for which interfaces are popular is that, a class cannot inherit more than one class but it can implement any number of interfaces
For Example, document can be an abstract class... passport, mark sheet etc can be the sub classes
Use:
Am going to tell only one use which i think is the most important use of abstract class. Almost every time a need for abstract class arises because of only this use,
It gives a protocol or rule like thing which every subclass should follow.
You should be clear with abstract classes now... let’s quickly move on to the final topic of the day.
Partial class:
As the name suggest a partial class is a type of class in which you have only partial implementation in one source file. Many such partial implementations of the same class can be present in different files. When running the application these partial implementations will be combined together by the compiler and seen as a single class !
In simple words they allow you to split a single class across files.
There are some general rules regarding these partial classes which i think you must know before you start using it...
- all fragments must have same access specifiers
- if any fragment is declared abstract / sealed then whole class becomes abstract
- if any part implements an interface the whole class has that implementation...so each fragment having some implementations will lead to the class having implementation of all the interfaces
- if some fragment inherits a base class then all other fragments also automatically inherit that base class
Uses of splitting the class is limited only to the imagination of the developers.
Common use is that it supports parallel development...
For example, we have a bird class code to make it fly is being written by a person and code to make it eat is written by someone else.. then partial classes can be used and development can be done parallelly. Of course that’s one simple example, it could be inefficient in many ways.
In real time, Visual studio uses partial class to separate auto generated code from user code. (same class is required in both these codes ! so partial class fitted perfectly in the scheme of things)
Difference between a structure and a class:
This is probably the first question everyone would be having in their mind after learning about classes. The answer is not straightforward as you expect. It’s a language specific question. In different languages we have different methods of implementation of classes and structures.
In C:
In C we don’t have classes. I will brief about structures now. Structures were introduced to group related data. Structures are like user defined variables. They cannot have functions or methods inside them. You cannot have access specifiers inside it and by default everything inside a structure can be accessed anywhere out.
In C++:
In C++ they extended structure in such a way that it could now have member functions within it.
- One main difference or probably the only difference between structure and class is that, in class default access specifier is private whereas in structure default access specifier is public.
- Similarly by default structure is inherited as public and class is inherited as private.
In C#:
Actually there are lot of differences between structure and class in C#. I will be mentioning a few below,
- Structures are value type whereas classes are reference type
Example:
Consider you have a structure called Hello and you have a class called World...
Now ,
Hello h; // this will create a object for the structure Hello
World w; //this will only create a reference(a pointer like thing)
w=new World(); //only now you will be allocated an object and w is a reference to that object
i guess things are clearer now.
So if you do copy operations the following happen...
Hello hcpy=h; //hcpy will be a duplicate object of h
World wcpy=w; // wcpy is just a copy of the reference w. so now wcpy and w will be pointing to the same object of World !!
Other few minor differences are,
- structures cannot have parameterless constructors
- structures cannot have destructors
See you in next post!
Trained @ Sourcebits
No comments:
Post a Comment