Latest Activity In Study Groups

Join Your Study Groups

VU Past Papers, MCQs and More

We non-commercial site working hard since 2009 to facilitate learning Read More. We can't keep up without your support. Donate.

CS304 GDB Solution & Discussion Fall 2015

Graded Discussion Board Dated: Jan 25, 16

Dear Students!

Please Note that GDB of CS304 will be launched on Monday, February 01, 2016(01-February-2016) and will last for 48 hours only.

GDB Topic:

Suppose you are working in an IT company that provides services to different education institutes to manage student’s record. Your company is going to launch a new project which requires saving information about student. Student information may include information related to their assignments, quizzes and exam marks or any other problem that student face in any course etc. Saved information can then be used later to analyze student data and find reasons if any student is not getting good marks. It is recommended to save the student records in different formats like saving in database, text files, xml and few others. Saving information in different formats will help the institutes analyze information as per their convenience. This information will be easily available to integrate with any application, institute is already using.

You are already working on this project as a software engineer in the company. . You know, there are different mediums to save the information like Save to DB, Save to File, etc. Similarly some other methods can be remove from file, remove from DB etc.

Now the student class can be implemented in two ways:

Either you can write abstract class with virtual functions like SaveToFile(), SaveToDB(), RemoveFromFile(), RemoveFromDB()etc.

You can develop an interface with declarations of the above mentioned functions.

Alternatively you can implement both in parallel. Which option will you adopt? Justify your answer with logical reasons.
Note: Try to provide precise, to the point answer and avoid irrelevant details.

Views: 5558

Replies to This Discussion

OK THANKS JI

The choice of whether to design your functionality as an interface or an abstract class (a MustInherit class in Visual Basic) can sometimes be a difficult one. An abstract class is a class that cannot be instantiated, but must be inherited from. An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all, thereby encapsulating common functionality for inherited classes. For details, see Abstract Classes.
An interface, by contrast, is a totally abstract set of members that can be thought of as defining a contract for conduct. The implementation of an interface is left completely to the developer.
Both interfaces and abstract classes are useful for component interaction. If a method requires an interface as an argument, then any object that implements that interface can be used in the argument.

Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.
If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

We cannot create an object of Abstract Class but we can create a reference of it.
An inheritance between abstract to abstract classes is possible. We don't need to implement abstract methods of the base abstract class into a derived abstract class. We can implement it later in concrete classes.
An abstract class can never be sealed or static.

An abstract class can have abstract as well as non abstract methods.

The abstract keyword can be used with class, methods, properties, indexers and events.

Abstract members can only be declared inside an abstract class.

An abstract member cannot be static or private.

An abstract method cannot be marked virtual.

A concrete class cannot inherit more than one abstract class, in other words multiple Inheritance is not possible.

Without an abstract class, we cannot implement the Template Method Pattern.

When designing applications, there are certain considerations you need to take care of when choosing whether you should use a abstract class or an interface. This choice of whether to design your functionality as an interface or an abstract class depends on certain factors. In this blog post I'll present a discussion on these points.

Abstract Class

First off, what is the difference between an abstract class and an interface? An abstract class is a special type of a class that cannot be instantiated. An abstract class should, however, be inherited by its subclasses. Abstract classes are either partially implemented or not implemented at all. You can have functionality in your abstract class -- the methods in an abstract class can be both abstract and concrete. An abstract class can have constructors -- this is one major difference between an abstract class and an interface. You may take advantage of abstract classes to design components and specify some level of common functionality in the methods of the abstract class until a specific implementation of the abstract class is needed in the class hierarchy.

Interface

An interface is basically a contract -- it doesn't have any implementation. An interface can contain only method declaration; it cannot contain method definition. You also can't have any member data in an interface. You can only have declaration of events, methods, and properties in an interface. An abstract class, on the other hand, can contain method definitions, fields and also constructors. Methods declared in an interface should be implemented by the classes that implement the interface. Note that a class can implement more than one interface but extend only one class. The class that implements the interface should implement all the members of an interface. Like abstract classes, an interface cannot be instantiated.

Should I use an abstract class or an interface?

Abstract classes provide you the flexibility to have certain concrete methods and some other methods that the derived classes should implement. On the contrary, if you use interfaces, you would need to implement all the methods in the class that extends the interface. An abstract class is a good choice if you have plans for future expansion -- if a future expansion is likely in the class hierarchy. If you would like to provide support for future expansion when using interfaces, you'll need to extend the interface and create a new one.

On a different note, it is easy to add a new interface to the hierarchy if need be. However, if you already have an abstract class in your hierarchy, you can't add another, i.e., you can only add an abstract class if there is not one available. You should use an interface if you want a contract on some behavior or functionality. You would not want to use an interface when you need to write the same code for the interface methods. In this case, you should use an abstract class and define the method once and reuse it as needed. You can use interfaces to decouple your application's code from any particular implementation of it. You can also use an interface to restrict access to members of a particular type. I'll discuss more about interfaces and abstract classes in my future posts here.

The MSDN states: "By using interfaces, you can, for example, include behavior from multiple sources in a class. That capability is important in C# because the language doesn't support multiple inheritance of classes. In addition, you must use an interface if you want to simulate inheritance for structs, because they can't actually inherit from another struct or class."

(Your company is going to launch a new project which requires saving information about student. Student information may include information related to their assignments, quizzes and exam marks or any other problem that student face in any course etc. Saved information can then be used later to analyze student data and find reasons if any student is not getting good marks.)

is ko pro r dekho k,,,hme scenario me kha gya ha k ap jst assignmnt, quiz, etc ki info save kro,,,studrnt ki personal info ni...to interface me personal b a jti hn i thnk jo irrelevent hn..r hm se pooch agya ha ap kon sa  las use kren ge,,for this info...so i think Abstract best ha,,,ab us se related info likh lo gdb me,,,5 marks ki ha

Answer: In this scenario i will prefer abstract class instead of interface, below are the reasons for choosing abstract class:

  1. Abstract class provides simple and easy way to version components.By updating the main class all inherited classes are automatically updated. interface cannot b changed once created.
  2. Abstract class is always a better solution for designing large functional units, as in given scenario.
  3. Abstract classes partially implement class on the other hand interfaces contain no implementation for any member.
  4. Change ker lena soneo...........

     According to this statement Abstract class is better than interface because Abstract classes can have implementations for some of its members, but the interface can't have implementation for any of its members and also it contains encapsulation. Abstract class contains the data member and member function. Interfaces cannot have fields where as an abstract class can have fields. An interface can inherit from another interface only and cannot inherit from an abstract class; where as an abstract class can inherit from another abstract class or another interface. A class can inherit from multiple interfaces at the same time, where as a class cannot inherit from multiple classes at the same time. Abstract can have access modifiers where as interface members cannot have access modifiers.

RSS

Looking For Something? Search Below

VIP Member Badge & Others

How to Get This Badge at Your Profile DP

------------------------------------

Management: Admins ::: Moderators

Other Awards Badges List Moderators Group

© 2021   Created by + M.Tariq Malik.   Powered by

Promote Us  |  Report an Issue  |  Privacy Policy  |  Terms of Service