| Start | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Overloading Methods Sometimes the same functionality has to be written for different kinds of inputs. At that time, one can use the same method name with a different set of parameters. Each different set of parameters is known as a method signature. As seen per the example, a single method can have multiple signatures. The advantage is that the same functionality is called with two different numbers of inputs. While invoking the method according to the input we are passing, (In this case either one string value or two string values) the corresponding method is executed. Methods can be overloaded: Note: Methods cannot be overloaded by changing just the return type ( |
||||||||||||
| Explaining what is method overloading and overriding. Method Overriding and Overloading are two forms of polymorphism supported by Java. Method Overloading Method overloading (also known as static Polymorphism) is a way you can have two (or more) methods (functions) with same name in a single class. Yes its as simple as that. This way user can call the same method for area depending on the type of shape it has. But the real question now is, how will java compiler will distinguish which method body is to be executed? Well Java have made it clear that even though the method names ( Overloaded methods must have different arguments list (quantity and types). That being said we cannot add another method to calculate area of a square like this : Thank god, there are some relaxations while writing overloaded methods like May have different return types. May have different access modifiers. May throw different exceptions. Why is this called static polymorphism? Well that's because which overloaded methods is to be invoked is decided at compile time, based on the actual number of arguments and the compile-time types of the arguments. One of common reasons of using method overloading is the simplicity of code it provides. For example remember Method Overriding Well, method overriding (yes you guess it right, it is also known as dynamic polymorphism) is somewhat more interesting and complex topic. In method overriding we overwrite the method body provided by the parent class. Got it? No? Let's go through an example. So we have a class called Shape and it has method called area which will probably return the area of the shape. Let's say now we have two classes called Circle and Rectangle. Similarly rectangle class: So, now both of your children classes have updated method body provided by the parent ( Wow! isn't it great? Two objects of same type calling same methods and returning different values. My friend, that's the power of dynamic polymorphism. Here's a chart to better compare the differences between these two:-
|
||||||||||||
Simplest Possible Class A class consists at a minimum of the You instantiate a class with the |
||||||||||||
| Object Member vs Static Member With this class: the following code snippet: produces this output: Note: You should not call
|
||||||||||||
| Basic Object Construction and Use Objects come in their own class, so a simple example would be a car (detailed explanations below): Objects are instances of their class. So, the way you would create an object would be by calling the Car class in one of two ways in your main class (main method in Java or onCreate in Android). Option 1 Option 1 is where you essentially tell the program everything about the Car upon creation of the object. Changing any property of the car would require calling one of the methods such as the Note: Make sure you pass the correct data type to the method. In the example above, you may also pass a variable to the That was an example of changing properties of an object, receiving properties of an object would require using a method from the Car class that has a return value (meaning a method that is not Option 1 is the best option when you have all the object's data at the time of creation. Option 2 Option 2 gets the same effect but required more work to create an object correctly. I want to recall this Constructor in the Car class: Notice that you do not have to actually pass any parameters into the object to create it. This is very useful for when you do not have all the aspects of the object but you need to use the parts that you do have. This sets generic data into each of the instance variables of the object so that, if you call for a piece of data that does not exist, no errors are thrown. Note: Do not forget that you have to set the parts of the object later that you did not initialize it with. For example, This is a common mistake amongst objects that are not initialized with all their data. Errors were avoided because there is a Constructor that allows an empty Car object to be created with stand-in variables ( And, as a reminder, get an object's properties by calling a method in your main class. Example: |
||||||||||||
| Constructors Constructors are special methods named after the class and without a return type, and are used to construct objects. Constructors, like methods, can take input parameters. Constructors are used to initialize objects. Abstract classes can have constructors also. It is important to understand that constructors are different from methods in several ways: Constructors also can be called through inheritance using the keyword See Java Language Specification #8.8 (opens new window) and #15.9 (opens new window) |
||||||||||||
| Initializing static final fields using a static initializer To initialize a Syntax
|
Read Article
https://develop.horje.com/learn/1434/reference