SlideShare a Scribd company logo
Working with Arrays of ObjectsLIS4930 © PICEverything comes out of an ArrayList<Object> as a reference of type Object, regardless of what the actual object is, or what the reference type was when you added the object to the list.ArrayList<Object>objectobjectobjectobject
What’s the Issue?LIS4930 © PICWhat type of object reference is the Practice array?What classes are these methods in?The compiler decides whether you can call a method based on the reference type, not the actual object type.
Get in touch with your inner Object.LIS4930 © PICObjectequals()getClass()hashCode()toString()DoctortreatPatient()FamilyDoctorgiveAdvice()There is ONE object on the heap here. A FamilyDoctor object. But it contains both the FamilyDoctor class parts of itself, the Doctor class parts, and the Object class parts of itself. FamilyDoctor object
‘Polymorphism’ means ‘many forms’LIS4930 © PICFamilyDoctor D = new FamilyDoctor();DObject F = D;FThe object reference can see ONLY the Object parts of the FamilyDoctor object. It can access only the methods of class Object. FamilyDoctor object
Casting an Object reference back to its real type.LIS4930 © PICFFamilyDoctor N = (FamilyDoctor) F;NIt’s still really a FamilyDoctor object, but if you want to call FamilyDoctor specific methods, you need a reference declared as type FamilyDoctorFamilyDoctor object
InstanceofLIS4930 © PICIf you’re not sure it’s a FamilyDoctor, you can use the instanceofoperator to check. Because if you’re wrong when you do the cast, you’ll get a ClassCastException at runtime and come to a grinding halt.
LIS4930 © PICTurn in your Textbooks.Look at page219 – 223in the textbook
Interface To The RescueLIS4930 © PICA Java interface solves your multiple inheritance problem by giving you much of the polymorphic benefits of multiple inheritance without the pain and suffering from the Deadly Diamond of Death (DDD).To make a Java interface you MUST make all the methods abstract!Use the keyword “implements” followed by the interface name. Note that when you implement an interface you still get to extend a classPetabstract void beFriendly()abstract void play()abstract void eat()To DEFINE an interface:public interface Pet { … }To IMPLEMENT an interface:public class Dog extends Canine implements Pet { … }
Building InterfacesLIS4930 © PICYou say ‘interface’ instead of ‘class’ herepublic interface Pet {	public abstract void beFriendly( );	public abstract void play( );}All interface methods are abstract, so they MUST end in semicolons.You say ‘implements’ followed by the name of the interface.You MUST give the methods a body here since they are abstract in the superclass.public class Dog extends Canine implements Pet {	public void beFriendly( ) { … }	public void play( ) { … }	public void roam( ) { … }	public void eat( ) { … }}
Classes from different inheritance trees can implement the same interface.LIS4930 © PICLook at page226in the textbookWhen you use a classas a polymorphic type the objects you can stick in that type must be from the same inheritance tree.But when you use an interfaceas a polymorphic type (like an array of Pets), the objects can be from anywhere in the inheritance tree. The only requirement is that the object are from a class that implements the interface.A class can implement multiple interfaces:public class Dog extends Animal implements Pet, Saveable, Paitable { … }
How do you know whether to make a class, a subclass, an abstract class, or an interface?LIS4930 © PICMake a class that doesn’t extend anything (other than Object) when your new class doesn’t pass the IS-A test for any other type.Make a subclass (extend a class) only when need to make a more specific version of a class and need to override or add new behaviors.Use an abstract class when you want to define a templatefor a group of subclasses, and you have at least some implementation code that all subclasses could use. Make the class abstract when you want to guarantee that nobody can make objects of that type.Use an interface when you want to define a role that other classes can play, regardless of where those classes are in the inheritance tree.
Invoking Superclass MethodsLIS4930 © PICThe keyword superlets you invoke a superclass version of an overridden method, from within the subclassLook at page228in the textbook

More Related Content

PPTX
#OOP_D_ITS - 8th - Class Diagram
PPTX
The Go Programing Language 1
PPTX
Variables in python
PPTX
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
DOC
PDF
Java OOP Programming language (Part 6) - Abstract Class & Interface
ODP
Beginners Guide to Object Orientation in PHP
PPTX
Php oop presentation
#OOP_D_ITS - 8th - Class Diagram
The Go Programing Language 1
Variables in python
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
Java OOP Programming language (Part 6) - Abstract Class & Interface
Beginners Guide to Object Orientation in PHP
Php oop presentation

What's hot (20)

PPT
Encapsulation
PPTX
C# interview
PPTX
OCA Java SE 8 Exam Chapter 5 Class Design
PPTX
PPT
Java interface
PPTX
Inheritance Mixins & Traits
DOCX
Static keyword u.s ass.(2)
PPTX
the Concept of Object-Oriented Programming
PPTX
Polymorphism and interface in vb.net
PPT
Interface in java By Dheeraj Kumar Singh
PPTX
Java(Polymorphism)
PPTX
CLASS OBJECT AND INHERITANCE IN PYTHON
PPTX
Oops concepts
PPT
Chapter 9 Interface
PPTX
encapsulation
PPTX
OCA Java SE 8 Exam Chapter 2 Operators & Statements
PPTX
Encapsulation
DOCX
C# interview quesions
PPTX
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
PPTX
object oriented programing lecture 1
Encapsulation
C# interview
OCA Java SE 8 Exam Chapter 5 Class Design
Java interface
Inheritance Mixins & Traits
Static keyword u.s ass.(2)
the Concept of Object-Oriented Programming
Polymorphism and interface in vb.net
Interface in java By Dheeraj Kumar Singh
Java(Polymorphism)
CLASS OBJECT AND INHERITANCE IN PYTHON
Oops concepts
Chapter 9 Interface
encapsulation
OCA Java SE 8 Exam Chapter 2 Operators & Statements
Encapsulation
C# interview quesions
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
object oriented programing lecture 1
Ad

Similar to 11 interfaces (20)

PPT
Internet programming slide - java.ppt
PPTX
Java interfaces
PPT
20. Object-Oriented Programming Fundamental Principles
PPT
inner-classes-abstract-classevgdddfs.ppt
PPS
Jump start to OOP, OOAD, and Design Pattern
PPT
Jump Start To Ooad And Design Patterns
PPTX
Polymorphism in C# Function overloading in C#
PPT
Object Oriented Programming Concept.Hello
ODP
Ppt of c++ vs c#
PPTX
Design patterns(red)
PPT
06 abstract-classes
PPTX
Object oriented programming Fundamental Concepts
PPT
Interfaces &amp; Abstract Classes
PPSX
Oop features java presentationshow
PPTX
Lecture 18
PPT
20 Object-oriented programming principles
PPT
Visula C# Programming Lecture 8
PPT
C++ Interview Questions
Internet programming slide - java.ppt
Java interfaces
20. Object-Oriented Programming Fundamental Principles
inner-classes-abstract-classevgdddfs.ppt
Jump start to OOP, OOAD, and Design Pattern
Jump Start To Ooad And Design Patterns
Polymorphism in C# Function overloading in C#
Object Oriented Programming Concept.Hello
Ppt of c++ vs c#
Design patterns(red)
06 abstract-classes
Object oriented programming Fundamental Concepts
Interfaces &amp; Abstract Classes
Oop features java presentationshow
Lecture 18
20 Object-oriented programming principles
Visula C# Programming Lecture 8
C++ Interview Questions
Ad

More from Program in Interdisciplinary Computing (20)

11 interfaces

  • 1. Working with Arrays of ObjectsLIS4930 © PICEverything comes out of an ArrayList<Object> as a reference of type Object, regardless of what the actual object is, or what the reference type was when you added the object to the list.ArrayList<Object>objectobjectobjectobject
  • 2. What’s the Issue?LIS4930 © PICWhat type of object reference is the Practice array?What classes are these methods in?The compiler decides whether you can call a method based on the reference type, not the actual object type.
  • 3. Get in touch with your inner Object.LIS4930 © PICObjectequals()getClass()hashCode()toString()DoctortreatPatient()FamilyDoctorgiveAdvice()There is ONE object on the heap here. A FamilyDoctor object. But it contains both the FamilyDoctor class parts of itself, the Doctor class parts, and the Object class parts of itself. FamilyDoctor object
  • 4. ‘Polymorphism’ means ‘many forms’LIS4930 © PICFamilyDoctor D = new FamilyDoctor();DObject F = D;FThe object reference can see ONLY the Object parts of the FamilyDoctor object. It can access only the methods of class Object. FamilyDoctor object
  • 5. Casting an Object reference back to its real type.LIS4930 © PICFFamilyDoctor N = (FamilyDoctor) F;NIt’s still really a FamilyDoctor object, but if you want to call FamilyDoctor specific methods, you need a reference declared as type FamilyDoctorFamilyDoctor object
  • 6. InstanceofLIS4930 © PICIf you’re not sure it’s a FamilyDoctor, you can use the instanceofoperator to check. Because if you’re wrong when you do the cast, you’ll get a ClassCastException at runtime and come to a grinding halt.
  • 7. LIS4930 © PICTurn in your Textbooks.Look at page219 – 223in the textbook
  • 8. Interface To The RescueLIS4930 © PICA Java interface solves your multiple inheritance problem by giving you much of the polymorphic benefits of multiple inheritance without the pain and suffering from the Deadly Diamond of Death (DDD).To make a Java interface you MUST make all the methods abstract!Use the keyword “implements” followed by the interface name. Note that when you implement an interface you still get to extend a classPetabstract void beFriendly()abstract void play()abstract void eat()To DEFINE an interface:public interface Pet { … }To IMPLEMENT an interface:public class Dog extends Canine implements Pet { … }
  • 9. Building InterfacesLIS4930 © PICYou say ‘interface’ instead of ‘class’ herepublic interface Pet { public abstract void beFriendly( ); public abstract void play( );}All interface methods are abstract, so they MUST end in semicolons.You say ‘implements’ followed by the name of the interface.You MUST give the methods a body here since they are abstract in the superclass.public class Dog extends Canine implements Pet { public void beFriendly( ) { … } public void play( ) { … } public void roam( ) { … } public void eat( ) { … }}
  • 10. Classes from different inheritance trees can implement the same interface.LIS4930 © PICLook at page226in the textbookWhen you use a classas a polymorphic type the objects you can stick in that type must be from the same inheritance tree.But when you use an interfaceas a polymorphic type (like an array of Pets), the objects can be from anywhere in the inheritance tree. The only requirement is that the object are from a class that implements the interface.A class can implement multiple interfaces:public class Dog extends Animal implements Pet, Saveable, Paitable { … }
  • 11. How do you know whether to make a class, a subclass, an abstract class, or an interface?LIS4930 © PICMake a class that doesn’t extend anything (other than Object) when your new class doesn’t pass the IS-A test for any other type.Make a subclass (extend a class) only when need to make a more specific version of a class and need to override or add new behaviors.Use an abstract class when you want to define a templatefor a group of subclasses, and you have at least some implementation code that all subclasses could use. Make the class abstract when you want to guarantee that nobody can make objects of that type.Use an interface when you want to define a role that other classes can play, regardless of where those classes are in the inheritance tree.
  • 12. Invoking Superclass MethodsLIS4930 © PICThe keyword superlets you invoke a superclass version of an overridden method, from within the subclassLook at page228in the textbook