Please discuss the advantages and disadvantages of placing your classes within a single file or in separate files? Also, explain what is an interface and How does an interface differs from an actual class? |
Advantages and disadvantages of placing your classes within a single file or in separate files?
Keeping all classes in a single file-
1. makes it easier to navigate in the Solution Explorer
2. follows the most common convention, making it easier for new developers to find their way around
3. offers a better granularity when working with version control systems, especially those where you have a locking checkout mechanism for editing files; you need to lock only the code for the class you are editing.
Keeping all classes in a seperate file-
1. Code reusability gets easy when we keep classes in seperate files
2. Break the giant source file and follow the rule: put shared class together and independent class seperate to seggregate code and for easy readability
3. unorganized-code will have difficulty to be maintained. Break the code into modules put them into separate class file, so the code can be reused and component are neat to be included at the right place.
What is Interface?
Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract. It is used to achieve total abstraction.
Interfaces specify what a class must do and not how. It is the blueprint of the class.
Since languages does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance .
It can also be used to achieve loose coupling.
Interface also represents the IS-A relationship.
How does an interface differs from an actual class?
1. An interface contains behaviors that a class implements but a class describes the attributes and behaviors of an object.
2. All the members of the interface are public by default, but variables and methods in a class can be declared using any access specifier(public, private, default, protected).
3. Interface cannot contain constructors, but class can contain constructor.
4. An Inteface cannot be instantiated i.e, objects cannot be created, on the other hand a class can be instantiated i.e, objects of a class can be created.
5. Inteface supports multiple inheritance, but class does not support.
6. Interface cannot inherit a class, but class can be inherit another class.
Get Answers For Free
Most questions answered within 1 hours.