SlideShare a Scribd company logo
We Know This Is LegalLIS4930 © PICmyDogDogDogDog objectThese two are the same type.Dog myDog= new Dog()
AND we know this is legalLIS4930 © PICmyDogDogAnimalDog objectThese two are not the same type.Animal myDog= new Dog()
BUT is this legal?LIS4930 © PICmyDogAnimalAnimalAnimal objectThese two are the same type, but… what the heck does an Animal object look like?Animal myDog= new Animal()
Turn in your Textbooks.LIS4930 © PICLook at pages200 – 210in the textbook
Some classes should not be instantiated!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.You can still use that abstract type as a reference type – as a polymorphic argument or return type, or to make a polymorphic array.LIS4930 © PIC
Concrete vs. AbstractConcrete 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.An abstract class means that nobody can ever make a new instance of that class.LIS4930 © PIC
Making Classes AbstractLIS4930 © PICabstract public class Canine extends Animal{	public void roam();}public class MakeCanine {	public void go() {		Canine c;		c = new Wolf();c = new Canine();c.roam();	}}Compilation ErrorabstractAnimalmakeNoise()eat()sleep()roam()Canineroam()WolfmakeNoise()eat()
LIS4930 © PICMaking Methods Abstractabstract public class Canine extends Animal{	public abstract void roam();	public void hunt() {		//go hunting	}}public class Wolf extends Canine {	public void roam() {System.out.println(“Roam in MT”);	}		Canine c;c = new Wolf();c = new Canine();c.roam();c.hunt();	}}Compilation ErrorALL abstract methods MUST be overridden!abstractAnimalmakeNoise()eat()sleep()abstractCanineroam()hunt()WolfmakeNoise()eat()roam()

More Related Content

Ad

More from Program in Interdisciplinary Computing (20)

Ad

12 abstract classes

  • 1. We Know This Is LegalLIS4930 © PICmyDogDogDogDog objectThese two are the same type.Dog myDog= new Dog()
  • 2. AND we know this is legalLIS4930 © PICmyDogDogAnimalDog objectThese two are not the same type.Animal myDog= new Dog()
  • 3. BUT is this legal?LIS4930 © PICmyDogAnimalAnimalAnimal objectThese two are the same type, but… what the heck does an Animal object look like?Animal myDog= new Animal()
  • 4. Turn in your Textbooks.LIS4930 © PICLook at pages200 – 210in the textbook
  • 5. Some classes should not be instantiated!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.You can still use that abstract type as a reference type – as a polymorphic argument or return type, or to make a polymorphic array.LIS4930 © PIC
  • 6. Concrete vs. AbstractConcrete 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.An abstract class means that nobody can ever make a new instance of that class.LIS4930 © PIC
  • 7. Making Classes AbstractLIS4930 © PICabstract public class Canine extends Animal{ public void roam();}public class MakeCanine { public void go() { Canine c; c = new Wolf();c = new Canine();c.roam(); }}Compilation ErrorabstractAnimalmakeNoise()eat()sleep()roam()Canineroam()WolfmakeNoise()eat()
  • 8. LIS4930 © PICMaking Methods Abstractabstract public class Canine extends Animal{ public abstract void roam(); public void hunt() { //go hunting }}public class Wolf extends Canine { public void roam() {System.out.println(“Roam in MT”); } Canine c;c = new Wolf();c = new Canine();c.roam();c.hunt(); }}Compilation ErrorALL abstract methods MUST be overridden!abstractAnimalmakeNoise()eat()sleep()abstractCanineroam()hunt()WolfmakeNoise()eat()roam()