Classes and Objects in Java: Constructors, Overloading, Static Members
Classes and Objects in Java: Constructors, Overloading, Static Members
Classes and Objects in Java: Constructors, Overloading, Static Members
What is a Constructor?
Constructor is a special method that gets invoked automatically at the time of object creation. Constructor is normally used for initializing objects with default values unless different values are supplied. Constructor has the same name as the class name. Constructor cannot return values. A class can have more than one constructor as long as they have different signature (i.e., different input arguments syntax).
2
Defining a Constructor
There is NO explicit invocation statement needed: When the object creation statement is executed, the constructor method will be executed automatically.
3
Multiple Constructors
Sometimes want to initialize in a number of different ways, depending on circumstance. This can be supported by having multiple constructors having different input arguments.
Method Overloading
Constructors all have the same name. Methods are distinguished by their signature:
That means, a class can also have multiple usual methods with the same name.
Polymorphism
Allows a single method or operator associated with different meaning depending on the type of data passed to it. It can be realised through:
Defining the same method with different argument types (method overloading) polymorphism. The method body can have different logic depending on the date type of arguments.
9
Scenario
A Program needs to find a maximum of two numbers or Strings. Write a separate function for each operation.
In C:
int max_int(int a, int b) int max_string(char *s1, char *s2) max_int (10, 5) or max_string (melbourne, sydney) int max(int a, int b) int max(String s1, String s2) max(10, 5) or max(melbourne, sydney)
In Java:
10
// Compare.java: a class comparing different items class Compare { static int max(int a, int b) { if( a > b) return a; else return b; } static String max(String a, String b) { if( a.compareTo (b) > 0) return a; else return b; } public static void main(String args[]) { String s1 = "Melbourne"; String s2 = "Sydney"; String s3 = "Adelaide"; int a = 10; int b = 20;
System.out.println(max(a, b)); // which number is big System.out.println(max(s1, s2)); // which city is big System.out.println(max(s1, s3)); // which city is big } }
11
this keyword can be used to refer to the object itself. It is generally used for accessing class members (from its own methods) when they have the same name as those passed as arguments.
public class Circle { public double x,y,r; // Constructor public Circle (double x, double y, double r) { this.x = x; this.y = y; this.r = r; } //Methods to return circumference and area }
12
Static Members
Java supports definition of global methods and variables that can be accessed without creating objects of a class. Such members are called Static members. Define a variable by marking with the static methods. This feature is useful when we want to create a variable common to all instances of a class. One of the most common example is to have a variable that could keep a count of how many objects of a class have been created. Note: Java creates only one copy for a static variable which can be used even if the class is never instantiated.
13
Static Variables
public class Circle { // class variable, one for the Circle class, how many circles private static int numCircles = 0; private double x,y,r; // Constructors... Circle (double x, double y, double r){ this.x = x; this.y = y; this.r = r; numCircles++; } }
15
public class CountCircles { public static void main(String args[]){ Circle circleA = new Circle( 10, 12, 20); // numCircles = 1 Circle circleB = new Circle( 5, 3, 10); // numCircles = 2 } } circleA = new Circle(10, 12, 20) circleB = new Circle(5, 3, 10)
numCircles
16
Instance variables : One copy per object. Every object has its own instance variable.
17
Static Methods
A class can have methods that are defined as static (e.g., main method). Static methods can be accessed without using objects. Also, there is NO need to create objects. They are prefixed with keyword static Static methods are generally used to group related library functions that dont depend on data members of its class. For example, Math library functions.
18
19
They can only call other static methods. They can only access static data. They cannot refer to this or super (more later) in anyway.
20
Summary
Constructors allow seamless initialization of objects. Classes can have multiple methods with the same name [Overloading] Classes can have static members, which serve as global members of all objects of a class. Keywords: constructors, polymorphism, method overloading, this, static variables, static methods.
21