Rules of abstract class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • namdeva
    New Member
    • Jun 2017
    • 5

    Rules of abstract class

    what are the parameter of creating abstract class and how we use in real time ?
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    There are no "parameter" . You just put "abstract" in front of "class".
    It doesn't matter if you use it in real time or normal time.
    Just write a second class that extends your abstract class and implement the abstract method of your abstract class, That's all how to use it.

    Comment

    • namdeva
      New Member
      • Jun 2017
      • 5

      #3
      i am asking how to use abstract class and in which case i have to use abstract class what are the rules we have to follow before implementing abstract class.

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        The coding rule: if the class is abstract, you should start the class name with "Abstract".
        Use case: For example you have a problem that you can solve with the state pattern (see wiki)
        Then you have many states derived from one abstract State.
        Example:
        Let's say a wizard can transform himself into an animal.
        The you have classes BirdState, CatState and SnakeState. You should derive them all from class AbstractState.
        In Abstract state, you write an abstract method getName().
        So every derived class is forced to implement it. So if you write a new DogState, you MUST write the getName() method there, else you cannot compile.
        In AbstractState, you put the default behaviour, for example the method canFly() that returns false. Then you only override this method in all classes with anmals that can fly, in our case the BirdState.canFl y() retuns true. The good thing is that if you add your DogState, then you don not need to write the canFly() method. So if someone calls dog.canFly(), it returns false automatically, as defined in the abstract state.

        Comment

        Working...