We Know This Is Legal<br />LIS4930 © PIC<br />myDog<br />Dog<br />Dog<br />Dog object<br />These two are the same type.<br...
AND we know this is legal<br />LIS4930 © PIC<br />myDog<br />Dog<br />Animal<br />Dog object<br />These two are not the sa...
BUT is this legal?<br />LIS4930 © PIC<br />myDog<br />Animal<br />Animal<br />Animal object<br />These two are the same ty...
Turn in your Textbooks.<br />LIS4930 © PIC<br />Look at pages<br />200 – 210<br />in the textbook<br />
Some classes should not be instantiated!<br />In other words, to stop anyone from saying “new” on that type. By marking th...
Concrete vs. Abstract<br />Concrete classes are those that are specific enough to be instantiated. A concrete class just m...
Making Classes Abstract<br />LIS4930 © PIC<br />abstract public class Canine extends Animal<br />{<br />	public void roam(...
LIS4930 © PIC<br />Making Methods Abstract<br />abstract public class Canine extends Animal<br />{<br />	public abstract v...
Upcoming SlideShare
Loading in...5
×

12 abstract classes

483

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
483
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

12 abstract classes

  1. 1. We Know This Is Legal<br />LIS4930 © PIC<br />myDog<br />Dog<br />Dog<br />Dog object<br />These two are the same type.<br />Dog myDog= new Dog()<br />
  2. 2. AND we know this is legal<br />LIS4930 © PIC<br />myDog<br />Dog<br />Animal<br />Dog object<br />These two are not the same type.<br />Animal myDog= new Dog()<br />
  3. 3. BUT is this legal?<br />LIS4930 © PIC<br />myDog<br />Animal<br />Animal<br />Animal object<br />These two are the same type, but… what the heck does an Animal object look like?<br />Animal myDog= new Animal()<br />
  4. 4. Turn in your Textbooks.<br />LIS4930 © PIC<br />Look at pages<br />200 – 210<br />in the textbook<br />
  5. 5. Some classes should not be instantiated!<br />In other words, to stop anyone from saying “new” on that type. By marking the class as abstract, the compiler will stop any code, anywhere, from ever creating an instance of that type.<br />You can still use that abstract type as a reference type – as a polymorphic argument or return type, or to make a polymorphic array.<br />LIS4930 © PIC<br />
  6. 6. Concrete vs. Abstract<br />Concrete classes are those that are specific enough to be instantiated. A concrete class just means that it’s OK to make objects of that type.<br />An abstract class means that nobody can ever make a new instance of that class.<br />LIS4930 © PIC<br />
  7. 7. Making Classes Abstract<br />LIS4930 © PIC<br />abstract public class Canine extends Animal<br />{<br /> public void roam();<br />}<br />public class MakeCanine {<br /> public void go() {<br /> Canine c;<br /> c = new Wolf();<br />c = new Canine();<br />c.roam();<br /> }<br />}<br />Compilation Error<br />abstract<br />Animal<br />makeNoise()<br />eat()<br />sleep()<br />roam()<br />Canine<br />roam()<br />Wolf<br />makeNoise()<br />eat()<br />
  8. 8. LIS4930 © PIC<br />Making Methods Abstract<br />abstract public class Canine extends Animal<br />{<br /> public abstract void roam();<br /> public void hunt() {<br /> //go hunting<br /> }<br />}<br />public class Wolf extends Canine <br />{<br /> public void roam() {<br />System.out.println(“Roam in MT”);<br /> }<br /> Canine c;<br />c = new Wolf();<br />c = new Canine();<br />c.roam();<br />c.hunt();<br /> }<br />}<br />Compilation Error<br />ALL abstract methods MUST be overridden!<br />abstract<br />Animal<br />makeNoise()<br />eat()<br />sleep()<br />abstract<br />Canine<br />roam()<br />hunt()<br />Wolf<br />makeNoise()<br />eat()<br />roam()<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×