Uses of Abstract Classes in Java -
this question has answer here:
in stackoverflow have got link answer below definition , concept of abstract classes provided not logical reason
in understanding think abstract classes contain implemented methods reusable subclasses.
i checked abstractlist , abstractmap, methods of them overridden in arraylist , hashmap same implemented methods of abstractlist or abstractmap not used in arraylist or hashmap.
is there more abstract classes. can let me know
in fact abstract classes may or may not include abstract methods. abstract classes cannot instantiated, can subclassed.
why use abstract classes ?
suppose modeling behavior of humans, creating class hierachy started base class called human
. human capable of doing different things speaking , walking , eating. let's take speaking behavior (example : greeting()
method) every humain depending on nationality have different language : english men : "hello" , frensh men : "bonjour" .
so know human can greeting different language !
candidate abstract method (forcing subclasses provide custom implementation). let's @ primitive humain base class, defines abstract method making greeting , walk :
public abstract class human { //we know humain walk same way provide //method implementation @ level public void walk() { system.out.println('i\'m walking !'); } //we don't know @ level what's language of humain public abstract void greet (); } //this class inherit walk() method , provide it's //specific implementation greeting() method public class englishmen extends human { @override public abstract void greet () { system.out.println('hello :) '); } } //this class inherit walk() method , provide it's //specific implementation greeting() method public class frenshmen extends human { @override public abstract void greet () { system.out.println('bonjour :) '); } }
now, humaun
wants instantiated (like englishmen or frenshmen) must implement greeting
method - otherwise impossible create instance of class.
note: when abstract class subclassed, subclass provides implementations of abstract methods in parent class. however, if not, subclass must declared abstract.
Comments
Post a Comment