Beruflich Dokumente
Kultur Dokumente
BITS Pilani
Hyderabad Campus
BITS Pilani
Hyderabad Campus
Todays Agenda
Methods Argument passing Recursion Access Control Understanding static and final Arrays
Overloading methods
In same class, if name of the method remains common but the number and type of parameters are different, then it is called method overloading in Java. Javas polymorphism. Java uses the type and / or number of arguments as its guide to determine which version of the methods to be used. Overloaded methods
Method names are same. Differ in type and / or number of parameters. Return type may or may not be same. (this alone is not enough)
Overloaded constructors
class Box { double width; double height; double depth; double volume() { return (width *height*depth); } Box() { System.out.println("object created with no dimensions"); width=-1; height=-1; depth=-1; } Box(double w,double h,double d) { System.out.println("object created with parameters passed"); width=w; height=h; depth=d; } Box(double len) { width = height = depth = len; } }
Argument passing
Call by value : when you pass a simple type to a method, it is call by value. Call by reference: Objects are passed by reference
Recursion
Java supports recursion.
Access control
Encapsulation links data with code that manipulates it. Encapsulation allows you to control what parts of the program can access members of a class. Used to prevent misuse.
Access Specifiers
Access Specifier defines the boundary and scope to access the method, variable and class etc. Java has define four type of access specifier such as: public private protected default
public specifiers
Public Specifiers achieves the highest level of accessibility. Classes, methods, and fields declared as public can be accessed from any class in the Java program, whether these classes are in the same package or in another package. public class Demo // public class { public x, y, size; // public instance variables }
Private Specifier
Private Specifiers achieves the lowest level of accessibility. Private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within subclasses and are not inherited by subclasses. So, the private access specifier is opposite to the public access specifier.
Private Specifier
public class Demo { // public class private double x, y; // private (encapsulated) instance v ariables public set(int x, int y) { // setting values of private fields this.x = x; this.y = y; } public get() { // setting values of private fields return Point(x, y); } }
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
Protected Specifier
Methods and fields declared as protected can only be accessed by the subclasses in other package or any class within the package of the protected members' class. The protected access specifier cannot be applied to class and interfaces.
Default specifier
When you don't set access specifier for the element, it will follow the default accessibility level. Classes, variables, and methods can be default accessed. Using default specifier we can access class, method, or field which belongs to same package, but not from outside this package.
Access Specifiers
The four access levels are: Visible to the package. the default. No modifiers are needed. Visible to the class only (private). Visible to the world (public). Visible to the package and all subclasses (protected).
The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.
Example
public class Customer { private String email; String position; //no modifier = package access modifier protected String name; public String city; }
Static keyword
Static keyword can be used with variables, methods and block of code. It is a variable which belongs to the class and not to object(instance) Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables A single copy to be shared by all instances of the class A static variable can be accessed directly by the class name and doesnt need any object Syntax : <class-name>.<variable-name>
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
static method
It is a method which belongs to the class and not to the object(instance) A static method can access only static data. It can not access non-static data (instance variables) A static method can call only other static methods and can not call a non-static method from it. A static method can be accessed directly by the class name and doesnt need any object Syntax : <class-name>.<method-name> A static method cannot refer to this or super keywords in anyway.
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
Static block
Static keyword can be applied to a block, { }, and they become static initialization block which runs only once when the class is first loaded by the JVM. This is used for initializing all the static variables and to perform any operations need to be executed when the class is loaded. They dont return anything. They dont take any arguments. They are executed in the order in which they appear in the class, i.e.) from top to bottom
Final
Final in java is very important keyword and can be applied to class, method, and variables in Java. Any variable either member variable or local variable (declared inside method or block) modified by final keyword is called final variable. Final variables are often declare with static keyword in java and treated as constant. Example final int hoursInDay=24;
Arrays
Using and array in your program is a 3 step process 1) Declaring your Array int[ ] aryNums; 2) Constructing your Array aryNums = new int[6]; 3) Initializing your Array aryNums[0] = 10; int[ ] aryNums = { 1, 2, 3, 4 }; String[ ] aryStrings = {"Autumn", "Spring", "Summer", "Winter" }; boolean[ ] aryBools = new boolean[ ] {false, true, false, true};
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
Multidimensional Arrays
Ex: int twoD[ ][ ] = new int[4][5] ; When you allocate memory for a multidimensional array, you need only specify the memory for the first (leftmost) dimension.
Array of objects
It is possible to declare array of reference variables. Class = new Class[array_length] Account obj[] = new Account[2]
double[] ar = {1.2, 3.0, 0.8}; int sum = 0; for (double d : ar) { // d gets successively each value in ar. sum += d; } double[] ar = {1.2, 3.0, 0.8}; int sum = 0; for (int i = 0; i < ar.length; i++) { // i indexes each element successively. sum += ar[i]; }
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
Nested class behaves just like any other member of its enclosing(outer) class. It has access to all the members of its enclosing class.
class A { class B { } }
Inner Class
We define the term "inner class" to the nested class that is:
declared inside the body of another class. not declared inside a method of another class. not a static nested class. not an anonymous inner class.
Example
public class InnerClass { static class StaticInner { static int i = 9; int no = 6; private void method() {} public void method1() {} static void method2() {} final void method3() {} } } The static inner class can be accessed from Outer Class in the following manner: InnerClass.StaticInner staticObj= new InnerClass. StaticInner (); No outer class instance is required to instantiate the nested static class because the static class is a static member of the enclosing class.
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
Example
public class InnerClass { int i = 9; public void method1() { final int k = 6; class MethodLocal { MethodLocal() { System.out.println(k + i); } } } }
Summary
Methods provide functionality to the class and use the class data. Methods can be overloaded only if the number of parameters are different. Methods can be static and can access only static variables. Access specifiers add encapsulation to the class, methods or instance variables. Inner class is enclosed in the scope of outer class.