SlideShare una empresa de Scribd logo
1 de 12
Working with Arrays of Objects LIS4930 © PIC Everything 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> object object object object
What’s the Issue? LIS4930 © PIC What 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 © PIC Object equals() getClass() hashCode() toString() Doctor treatPatient() FamilyDoctor giveAdvice() 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 © PIC FamilyDoctor D = new FamilyDoctor(); D Object F = D; F The 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 © PIC F FamilyDoctor N = (FamilyDoctor) F; N It’s still really a FamilyDoctor object, but if you want to call FamilyDoctor specific methods, you need a reference declared as type FamilyDoctor FamilyDoctor object
Instanceof LIS4930 © PIC If 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 © PIC Turn in your Textbooks. Look at page 219 – 223 in the textbook
Interface To The Rescue LIS4930 © PIC A 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 class Pet abstract 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 Interfaces LIS4930 © PIC You say ‘interface’ instead of ‘class’ here public 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 © PIC Look at page 226 in the textbook When 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 © PIC Make 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 Methods LIS4930 © PIC The keyword superlets you invoke a superclass version of an overridden method, from within the subclass Look at page 228 in the textbook

Más contenido relacionado

La actualidad más candente

OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Designİbrahim Kürce
 
Static keyword u.s ass.(2)
Static keyword u.s ass.(2)Static keyword u.s ass.(2)
Static keyword u.s ass.(2)Syed Umair
 
the Concept of Object-Oriented Programming
the Concept of Object-Oriented Programmingthe Concept of Object-Oriented Programming
the Concept of Object-Oriented ProgrammingAida Ramlan II
 
Polymorphism and interface in vb.net
Polymorphism and interface in vb.netPolymorphism and interface in vb.net
Polymorphism and interface in vb.netKarthigaGunasekaran1
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singhdheeraj_cse
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONLalitkumar_98
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 InterfaceOUM SAOKOSAL
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statementsİbrahim Kürce
 
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods EncapsulationOCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods Encapsulationİbrahim Kürce
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1Geophery sanga
 

La actualidad más candente (20)

Encapsulation
EncapsulationEncapsulation
Encapsulation
 
C# interview
C# interviewC# interview
C# interview
 
OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Design
 
Oops
OopsOops
Oops
 
Java interface
Java interfaceJava interface
Java interface
 
Inheritance Mixins & Traits
Inheritance Mixins & TraitsInheritance Mixins & Traits
Inheritance Mixins & Traits
 
Static keyword u.s ass.(2)
Static keyword u.s ass.(2)Static keyword u.s ass.(2)
Static keyword u.s ass.(2)
 
the Concept of Object-Oriented Programming
the Concept of Object-Oriented Programmingthe Concept of Object-Oriented Programming
the Concept of Object-Oriented Programming
 
Polymorphism and interface in vb.net
Polymorphism and interface in vb.netPolymorphism and interface in vb.net
Polymorphism and interface in vb.net
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
 
Oops concepts
Oops conceptsOops concepts
Oops concepts
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
 
encapsulation
encapsulationencapsulation
encapsulation
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statements
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
C# interview quesions
C# interview quesionsC# interview quesions
C# interview quesions
 
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods EncapsulationOCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
 

Similar a 11 interfaces

Internet programming slide - java.ppt
Internet programming slide - java.pptInternet programming slide - java.ppt
Internet programming slide - java.pptMikeAdva
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental PrinciplesIntro C# Book
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternNishith Shukla
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsLalit Kale
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Abid Kohistani
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classesAnup Burange
 
Object oriented programming Fundamental Concepts
Object oriented programming Fundamental ConceptsObject oriented programming Fundamental Concepts
Object oriented programming Fundamental ConceptsBharat Kalia
 
Interfaces &amp; Abstract Classes
Interfaces &amp; Abstract ClassesInterfaces &amp; Abstract Classes
Interfaces &amp; Abstract ClassesBharat17485
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
20 Object-oriented programming principles
20 Object-oriented programming principles20 Object-oriented programming principles
20 Object-oriented programming principlesmaznabili
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Abou Bakr Ashraf
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oopcolleges
 

Similar a 11 interfaces (20)

Internet programming slide - java.ppt
Internet programming slide - java.pptInternet programming slide - java.ppt
Internet programming slide - java.ppt
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design Patterns
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
 
12 constructors
12 constructors12 constructors
12 constructors
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
Object
ObjectObject
Object
 
Design patterns(red)
Design patterns(red)Design patterns(red)
Design patterns(red)
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Object oriented programming Fundamental Concepts
Object oriented programming Fundamental ConceptsObject oriented programming Fundamental Concepts
Object oriented programming Fundamental Concepts
 
Interfaces &amp; Abstract Classes
Interfaces &amp; Abstract ClassesInterfaces &amp; Abstract Classes
Interfaces &amp; Abstract Classes
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
20 Object-oriented programming principles
20 Object-oriented programming principles20 Object-oriented programming principles
20 Object-oriented programming principles
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8
 
C++ Interview Questions
C++ Interview QuestionsC++ Interview Questions
C++ Interview Questions
 
L04 Software Design 2
L04 Software Design 2L04 Software Design 2
L04 Software Design 2
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
 

Más de Program in Interdisciplinary Computing (20)

Phpmysqlcoding
PhpmysqlcodingPhpmysqlcoding
Phpmysqlcoding
 
Database basics
Database basicsDatabase basics
Database basics
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
01 intro tousingjava
01 intro tousingjava01 intro tousingjava
01 intro tousingjava
 
Web architecture v3
Web architecture v3Web architecture v3
Web architecture v3
 
Xhtml
XhtmlXhtml
Xhtml
 
Webdev
WebdevWebdev
Webdev
 
Web architecture
Web architectureWeb architecture
Web architecture
 
Sdlc
SdlcSdlc
Sdlc
 
Mysocial
MysocialMysocial
Mysocial
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Html5
Html5Html5
Html5
 
Frameworks
FrameworksFrameworks
Frameworks
 
Drupal
DrupalDrupal
Drupal
 
Database
DatabaseDatabase
Database
 
Javascript2
Javascript2Javascript2
Javascript2
 

11 interfaces

  • 1. Working with Arrays of Objects LIS4930 © PIC Everything 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> object object object object
  • 2. What’s the Issue? LIS4930 © PIC What 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 © PIC Object equals() getClass() hashCode() toString() Doctor treatPatient() FamilyDoctor giveAdvice() 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 © PIC FamilyDoctor D = new FamilyDoctor(); D Object F = D; F The 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 © PIC F FamilyDoctor N = (FamilyDoctor) F; N It’s still really a FamilyDoctor object, but if you want to call FamilyDoctor specific methods, you need a reference declared as type FamilyDoctor FamilyDoctor object
  • 6. Instanceof LIS4930 © PIC If 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 © PIC Turn in your Textbooks. Look at page 219 – 223 in the textbook
  • 8. Interface To The Rescue LIS4930 © PIC A 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 class Pet abstract 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 Interfaces LIS4930 © PIC You say ‘interface’ instead of ‘class’ here public 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 © PIC Look at page 226 in the textbook When 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 © PIC Make 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 Methods LIS4930 © PIC The keyword superlets you invoke a superclass version of an overridden method, from within the subclass Look at page 228 in the textbook