Showing posts with label OOP TUTORIAL. Show all posts
Showing posts with label OOP TUTORIAL. Show all posts

Thursday, 15 October 2015

Tutorial On This Keyword In Java

This keyword in java has many features but in this tutorial we are going to discussed specific feature of this keyword.In this tutorial we tried show how this keyword is used to refer current instance variable in this case this keyword is used when we want to use same name of both insatance varaible and local variable then we used this keyword with to distinguish between them.following example illustrates this keyword.

CODING:

class current
{
double current;
double resistance;
//Creating Parameterize constructor
current (double current,double resistance)
{
this.current = current;
this.resistance = resistance;
}

double volt() 
{
return current*resistance;
}
}

class voltage
{
public static void main(String args[])
{
//Creating Object Of current class
current cur=new current(10,40);
current cur1=new current(30,10);
System.out.println("Voltage is = "+cur.volt()+" volt");
System.out.println("Voltage is = "+cur1.volt()+" volt");
}
}  

OUTPUT:

voltage is=400volt
voltage is=300volt



Tuesday, 14 July 2015

How To Perform Method Overloading In Java

Method overloading is one of the most amazing feature of OOP method overloading in java can be defined as  a class having more than one method having same name but having different input parameters.Its make easier for user because if user uses different names for all method program will become difficult for a user to understand program.Lets discuss what are require for method overloading.

HOW TO OVERLOAD METHODS IN JAVA:

Method can be simply be overloaded by using one rule.
  • Number of input parameter of method should be different from others.As we done in below program.
EXAMPLE:
void input()
void input(int a)

  • Parameter data type should be different from others for example if one method takes double so other should not use double data type as a parameter.
EXAMPLE:
void input(double a)
void input(int a)

NOTE:Method can not be overloaded by changing return type of method.

CODING:

class overloading
{
void input()
{
System.out.println("No Parameter");
}
//overload method for one parameter 
void input(int a)
{
System.out.println("a="+a);
}
//overload method for two parameter 
void input(int a,int b)
{
System.out.println("a = "+a+" b= "+b);
}

//overload method for returning double parameter
double input(double a)
{
System.out.println("Double Value of a="+a);
return a*a;
}
}
class mainoverload
{
public static void main(String arg[])
{
overloading x = new overloading();
//calling all version of input methods
x.input();
x.input(50);
x.input(100,30);
double ans=x.input(123.4567);
System.out.print("Result of x.input(123.4567)="+ans);
}
}

OUTPUT:

No parameter
a = 100 b=30
Double Value of a = 123.4567
Result of x.input(123.4567)=15241.55677489






Sunday, 12 July 2015

Tutorial On Parametrized Methods In Java

Before reading this tutorial you should read Tutorial on method in java for better understanding of parametrized method in java.

PARAMETRIZED METHOD:

Method that is used to for passing information to a  using parameters is called parametrized method for passing a information using method require two things first is type and second is variable name such as String n.Any type of data can be used as a parameter in  in method such as integer,double and float.Array and object can also be used as a parameter as shown in below.
book(String n[]).

NOTE:One Should be kept in mind thatwe cannot use reserved keyword as a parameter name.

CODING:

class book
{
String name,editor;
float prize;
int quantity;

void input(String n,String e,int q,float p)
{
name=n;
editor=e;
quantity=q;
prize=p;
}
void output()
{
System.out.println("Book Name= "+name);
System.out.println("Editor= "+editor);
System.out.println("Quantity= "+quantity);
System.out.println("Prize= "+prize+"$");
System.out.println();
}
}
class bookmain
{
public static void main(String arg[])
{
book information=new book();
information.input("Electronic Devices","Robert Boylestad",4,60);
information.output();
information.input("Electric Circuit","F.Bogart",10,35);
information.output();
information.input("Business","Ronald J.Ebert",2,15);
information.output();

}
}

OUTPUT:




Wednesday, 8 July 2015

Using Methods For Returning A Value In Java

Before reading this tutorial you should have enough knowledge regarding methods of java so first see our tutorial Tutorial On simple method in java.

RULES OF RETURNING VALUE FROM METHOD:

For returning a value from method a following conditions have to be fulfilled 
  • Method Should have a return statement.
  • Return type should be declare in a method as I have done in this program double volt() its mean that method with void declaration can not return any value.
  • Return data type value should match the declared data type such as double is declared as a return type in this program so we can not return an int type data from method..

CODING:

class current
{
double current;
double resistance;
double volt() 
{
return current*resistance;
}
}

class voltage
{
public static void main(String args[])
{
//Creating Object Of current class
current cur=new current();
//assiging value of current and resistance in instance variable
cur.current=30;
cur.resistance=100;
System.out.println("Voltage is = "+cur.volt());
}
}  

OUTPUT: