vivarem logo
/ Vivarem / Computers and IT / IT Education / online / tutorial / Java / OOP / 4 /
There are 0 sub sections

You can add Your Link here with New Link
Java Object Oriented Programming - 4

Function Overloading , Access specifiers and modifiers
Function Overloading
You can use same functionname for different functions which differ in paramenters,types, and numberof paramenter, it may or may not have same return type as the case may be
Example

package com.vivarem.examples;
public class JavaFunctionOverloading{
double a,b;
public JavaFunctionOverloading(double a,double b){this.a=a;this.b=b;}
public JavaFunctionOverloading(){
a=0;b=10;
}
public double sum(){return a+b;}
public int sum(int a,int b){ return a+b;}
public double sum(double a,double b){ return a+b;}
public static void main(String[] a){
JavaFunctionOverloading jfo=new JavaFunctionOverloading();
System.out.println("double sum() "+jfo.sum());
System.out.println("double sum(double,double) "+jfo.sum(10.5,14.2));
System.out.println("int sum(int,int) "+jfo.sum(2,3));
jfo=new JavaFunctionOverloading(200.0,100.0);
System.out.println("overloaded constructor and double sum()"+jfo.sum());

}
}


Access specifiers and modifiers
There are 4 access specifiers, public, default, protected,private.when used with functions and data members, its helps in datahiding and thus provides security.we will discuss each of them in detail while discussing inheritance. private accessible only within the class, default within the class and inheriting classes of same packages,protected accessible iwthin the class and any inheriting classes.public accessible from anywhare.

There are 3 important modifiers static, final, transient.

when static is prefixed to a datamember or function it means that the particular data/function is part of a class rather than an object.you can access datamember/function through class directly.

General syntax of static function
[access specifier] static return_type funcionname(){

}

General general syntax Static datamember declaration
[access specifier] static type varname;

Calling static function
ClassName.staticFunctionName.

When a datamember is declared final it means it is a constant and the value cannot be changed
e.g. final int i=10;

When a variable is declared transient it means the variable cannot be serialized or value cannot be stored persistantly.we will look into this in detail when discussing serialization.

< Control Structures |
Content |
Arrays >


You can Add a Classified here with New Classified