Presentation is loading. Please wait.

Presentation is loading. Please wait.

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.

Similar presentations


Presentation on theme: "OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path."— Presentation transcript:

1 OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path

2 The toString() Method: The toString() method is declared in the Object Class. It is commonly used by objects to return a String representation of themselves to display their current state. Because every Class in Java inherits from Object, every Class has access to the toString() method. The toString() method is commonly overridden in sub classes so that developers can decide how objects will be textually displayed at run time. Objects are commonly displayed textually through the System.out.println command or through List Boxes in GUI environments.

3 OO Concepts Polymorphism: It is important to understand what happens when a method call is made to objects of various types in an inheritance hierarchy. When you a call a method of a subclass using certain parameters, here is what happens: The subclass checks to see if it has that method with exactly the parameters specified. If it does it uses it. If the method is not found the super class is searched. If the method is found in a super class the method is executed. If the method is not found a compile time error occurs.

4 OO Concepts Polymorphism: An objects ability to decide what method to apply to itself in the inheritance hierarchy is called polymorphism. Methods with same signature (name and argument list) defined in an inheritance hierarchy is called method overriding. Objects in an inheritance hierarchy with overridden methods can react differently to the same method call. Polymorphism is also the ability of objects in an inheritance hierarchy to react differently to the same method call.

5 OO Concepts Dynamic Binding: Dynamic binding is very closely related to polymorphism in Java. Dynamic binding is the process of resolving the reference to a method at run time. When you call a method that has been overridden by at least one subclass in an inheritance hierarchy, the JVM determines which version of the method to call. You do not have to do any special coding to take advantage of dynamic binding because it is automatic.

6 OO Concepts Interfaces An interface is a special kind a Class that is used as a design template to specify what kind of behavior a Class must implement. An interface is a group of constants and method declarations that define the form of a class. However, interfaces provide no method implementation. In essence, an interface allows you to specify what a class must do, but not how it will get done. Abstract Classes use abstract methods to ensure that any Class inherited from the Abstract Class must implement that method.

7 OO Concepts Interfaces Interfaces are similar in that they define public abstract methods that provide no implementation. The Class implementing the interface is also forced to implement these methods. Interfaces can have fields, but they must be declared as static and final. If you omit the static and final qualifiers Java provides them for you.

8 OO Concepts Syntax for Interfaces: [public] interface interface_name [extends interface_name] { /*Declare fields and methods*/ } How do Classes Implement Interfaces? public class clasName implements interfaceName[,interfaceName] { /*Class Body*/ }

9 Class Path & Packages The CLASSPATH variable is how we tell applications written in the Java programming language where to look for user Classes. The Java interpreter (java) knows where to find the Core Java Classes that come with Java. The Java interpreter locates these classes needed to run your code by using the CLASPATH environment variable. We have used the Java Class String numerous times in our programs so far. The interpreter was able to find the String class because it is in the system environment variable called CLASSPATH.


Download ppt "OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path."

Similar presentations


Ads by Google