Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How do you know whether to make a class, a subclass, an abstract class, or an interface?

    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 556
    Answer it

    I usually write code as I go along, but I noticed this takes more time in the long run especially when I lose track of what codes does what where, but I wanted to start planning my code by making UML class diagrams and mockups.

    However, like the title says, how do you know whether to make a class, a subclass, an abstract class, or an interface is there some guide line or rule of thumb that most programmers follow.

    If someone can please explain to me the question above in simple terms that will be a great help.

 2 Answer(s)

  • Class, sub-class:

    Java is an Object oriented programming language. The thumb rule can be simply to have the idea of an “Object”. One must find out the three characteristic in an object . It's state, behavior and identity. Now if we go by the definition of a class, we classify different objects on the basis of their properties/attributes but value of different properties/attributes. “A class is a cluster of objects which have common properties.(Lets take an example of a project related to a transportation company that provide vehicles for transportation service. The company provides vehicles of different types as per the requirement of the customer which can be hired through online booking). As I said all the objects (such as two wheeler, three wheeler four wheeler) will have same property (size, speed etc.) but different values (lets say two wheeler can accommodate 2 person, three wheeler can accommodate 4 person). Now we classify all the mode of transportation into different categories under the parent class vehicles because our prime object is of type vehicle. Under this prime object we can again classify the objects on the basis of their properties like we can again classify 4 wheeler on the basis of size, speed, luxury. Hence when we see that there is a possibility of classification this is the situation where we find the need of creating sub class that will have the property and functionality of the parent but will introduce some more specifications.

    Abstract class:

    Making an abstract class needs the understanding of important things and focus mainly on “What” and avoid “How” from the operator/user prospective. Understanding that who is going to use the product and what are his/her requirements, accordingly hiding/displaying the implementation details and showing only the functionality to the user. A simple example of this can be a user writing and sending an email who is not bothered about how the mail will reach the destination. Lets take example of the above example class vehicle, which have a functionality of speed which we can declare as abstract as this functionality will be used by different objects (i.e. two/three/four wheelers)in different modes (i.e. slow, medium, fast).

    Interface:

    While developing a module or any section of a system, if a developer come across a situation where he/she thinks that a particular method is the necessity and has to be implemented by the other class i.e. the method must be abstract method within a class that is to be implemented by another class. It means that we have to make the class 100% abstract and when we want a class as 100% abstract we declare the class as Interface and not abstract.

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: