Lecture Ten More On Methods PDF

Summary

This lecture provides more information on methods in Java programming. Topics discussed include stubs, encapsulation, polymorphism, overloading, the copy rule, primitives, and reference variables.

Full Transcript

More on Methods TRACEY CASSELLS Stub  A stub is a simple but incomplete version of a method.  Stubs are often used in software development as a temporary placeholder for a method that has not yet been fully implemented.  A stub typically returns a hard-coded...

More on Methods TRACEY CASSELLS Stub  A stub is a simple but incomplete version of a method.  Stubs are often used in software development as a temporary placeholder for a method that has not yet been fully implemented.  A stub typically returns a hard-coded value or a default value. Stub  In eclipse you will often see  // TODO Auto-generated method stub  The IDE has generated the method structure for you and you need to add the actual implementation. Encapsulation  When we call a method we only know its signature.  The signature is just the method name and arguments  The details of the implementation are hidden from the client who invokes the method.  This is know as encapsulation. Encapsulation  When you call scanner.nextInt().  You know it returns an integer from the console.  But you don't know how.  And when you're just learning to code you don't really need to know the how. Encapsulation  By encapsulating the implementation we remove a lot of complexity.  If you had to understand the inner workings of every method to use them we would never get any work done. Polymorphism  Polymorphism allows an object to take different forms.  In java that means a method with the same name can behave differently depending on which type of object it is activated with. Overloading  When methods are overloaded, the method is defined with the same name but with different implementations.  The actual code for the method is attached or bound at compile time and because all of this happens in advance it is called static binding public static int add(int a, int b) { return a + b; } public static int add(int a, int b, int c) { Overloadi return a + b + c; ng } public static double add(double a, double b) { return a + b; } public static void main(String[] args) { int sum = add(2,4); sum = add(3,4,5); double fraction = add(4.5,5); } Overloading  To overload we need to either change  The number of parameters  The type of parameters  The signature of a method refers to its name and parameters  If a method has the same name and parameters as another method it's considered the same  It doesn’t matter if the return type is different.  public static void print(int num) { System.out.println(num); Overloadi } ng public static void print(long num) { If an argument can System.out.println(num); be applied to more than one datatype } then the narrower data type is chosen public static void main(String[] args) In tis case print(int { num) is called as an int argument print(5); can be implicitly converted to a } long parameter. Advantage of Polymorphism  Sometimes we have calculations that can take data in different forms, as in the previous example we want to be able to add both int's and doubles.  So instead of having addInts and and addDoubles we have add(int,int) , add(double,double) The copy rule  The Copy rule refers to the way in which variables and values are copied or referenced when they are assigned or passed as arguments to methods.  Primitive and immutable values are copied. All other objects are passed by reference. Primitive variables  When we create a primitive variable in java we create a direct link to its stored value  int num =5;  System.out.println(num);  //prints 5 Primitive variables  num reserves a space in memory large enough for an int so 16 bits are reserved.  An int is a primitive and has a concrete value.  When we use = 5 we put the value 5 in that reserved space. Primitive variables  Primitives are immutable.  When we pass a primitive variable into a method we pass a copy.  Any changes to the primitive in the method do not affect the original. Primitive variables Primitive variables  When we perform calculations on primitives *, / , +  num = num + 5;  We aren't changing the original value but creating a new one in its place. Reference variables  Anything in Java that isn't a primitive data type is a reference type.  String, Scanner, Array.  String is a reference type but is also an immutable class.  By default java sets a reference size to 16 bytes. Reference variables  When we create an object variable in java we create a reference to where its information is being stored.  int[] num = new int;  System.out.println(num);  //I@27f674d Reference variables  This information includes any internal variables and methods.  An object is mutable.  We can change the internal structure.  If we have an array we can update values stored in it.  int = 4; Reference variables  If we assign a reference variable it’s a direct reference.  If we pass a reference variable into a method it’s a direct reference not a copy.  Any changes made in the method will affect the original. Questions?

Use Quizgecko on...
Browser
Browser