OOP & Java: how to generalize from an abstract class? -


first of all, i'd apologize grammar, since english not first language, make grammar mistakes...

there problem prevent me making generic code:

how put abstract class abstract methods inside abstract class? know abstract classes can't instantiated, but... isn't there way genericity?

one example: have abstract class feline, class has several abstract methods. there class: cage class, can contain amount of 1 kind of feline objects (say cage of cats, cage of tiggers, cage of lions, etc.)... how do this? can't instantiate felines inside cage class... , how make cage of cats?

another example, i've noticed can sort collection of stuff using 2 sorting criteria: stuff goes after stuff? and: stuff goes before stuff?

so, every sortable stuff must have 2 boolean methods: "goesafter(stuff): boolean" , "goesbefore(stuff): boolean", then, put stuff on class through composition/agreggation relationship, lets call lotsofsortablestuff class, , put "sort(): void" method, method use goesafter , goesbefore methods sortable stuff...

there lots of things can sorted: studentes (by degree), childs (by height), names (by alfabetic order), etc... problem is: whenever want make collection of sortable stuff, have type on , on same 2 methods, , sorting algorythm!

how apply genericity in kind of problems?

polymorphism allows store objects of subclass in containers defined hold superclass objects. instance in class cage can have list<feline> felines can contain any object of subclass of feline

sorting: can make superclass implement comparator interface, requires concrete subclasses implement compareto method. instance if wanted sort cage above weight of cats.

here's example of might like:

  import java.util.list;   import java.util.arraylist;     import java.util.collections;    public abstract class feline implements comparable   {        protected double weight;          protected abstract void whoami();         public void compareto(feline other){           new double(this.weight).compareto(new double(other.weight));        }         public static void main(string[] args){           cage c = new cage();            feline l = new lion(400.0);           feline t = new tiger(555.0);            c.addfeline(l);           c.addfeline(t);            for(feline f : c.getcage()){              f.whoami();           }        }    }      class tiger extends feline      {        public tiger(double weight){           super(weight);        }               protected void whoami() {           system.out.println("i'm tiger");        }     }      class lion extends feline      {        public lion(double weight){           super(weight);        }         protected void whoami() {           system.out.println("i'm lion");        }     }      class cage     {         private list<feline> felines;           public cage(){            this.felines = new arraylist<>();         }          public void addfeline(feline f){             this.felines.add(f);          }          public list<feline> getcage(){            return this.felines;         }          public void sort(){            collections.sort(this.felines);         }     } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -