Java Type Substitution Principle

subtype substitution principle n.w
1 / 5
Embed
Share

Understanding Java type substitution principle including static and dynamic types, subtyping relationships, and compiler behavior to prevent runtime errors. Learn how to apply subtype substitution in Java code for better flexibility and compatibility.

  • Java Programming
  • Subtype Substitution
  • Compiler Errors
  • Type Checking
  • Object-Oriented Programming

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Subtype Substitution Principle

  2. Type Every variable in Java has a type. Specifically, every reference variable has two types: static type and dynamic type. A type in Java can be one of the following: Primitive type: int, float, double, boolean, and char. Any classes: Built-in classes: Object, String, and Integer etc. User-defined classes: Student, Person, and University etc. Interfaces 9-2

  3. Subtype Type B is a subtype of Type A if A is an interface and BimplementsA, OR, A are B both are interfaces and B extends A, OR, A is a class and B extends A A A OR B B As special case, any type A is a subtype of A

  4. How Does Compiler Do? Recall compiler tries to detect as many runtime errors as possible. Static Type checking is one of the ways for compiler to avoid runtime errors. Why the static type of the RHS of assignment should be a subtype of the static type of the LHS of assignment.

  5. Subtype Type Substitution If type B is a subtype of type A and everywhere the code expects A, then a B can be used instead. The code expects A follows the first write first dominate (FWFD) principle. For instance: public Person m(Student s) { return new Student(); } Person p = new Student(); producer x.m(new Collegestudent ()); client/consumer

More Related Content