methods and constructors in java

Post on 10-Nov-2014

837 Views

Category:

Education

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

Methods and constructors in java

TRANSCRIPT

Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Methods and constructors in java

Shanu k kshanukk89@gmail.comwww.facebook.com/shanunnitwitter.com/shanuin.linkedin.com/in/shanu k k9656153432

Week Target Achieved

1 25 25

2 27 30

3 35 29

Typing Speed

Jobs Applied# Company Designation Applied Date Current Status

1

2

3

MethodoA Java method is a collection of

statements that are grouped together to perform an operation

oA class without method has no life,the object created by such class cannot respond to any message

oMethods are declared inside the body of the class but immediately after the declaration of the instance variable

Method declaration

Type methodname(parameter_list){

Method_body;}

Eg:Class rectangle{int length, width;Void getData(int x,int y){Length=x;Width=y;}}

class sum {

void sum(int a,int b){System.out.println( (a+b));}void sum(int a,int b,int c){System.out.println( (a+b+c));}public static void main(String args[]){sum obj=new sum();obj.sum(10,5);obj.sum(10,5,10);}

}

Constructor:oObject we created must be given to instial

valuesoWe have done earlier using two approaches

1 Dot operator:access the instance variable and assign value them indiviidualy

2 getData method:intialise each object individually

it would be simpler to initialise an object when it’s first created _special type of method is called construcor

• Constructor have the same name as the class itself

• They do not specify any return type not even void

• class Constructor Demo{ Constructor Demo(){ System.out.println("Inside no argument constructor"); } ConstructorDemo(String name){ System.out.println("Inside one argument constructor in Java with name: " + name); }

public static void main(String args[]) { Constructor Demo d = new ConstructorDemo(); //calling no argument constructor in java Constructor Demo e = new ConstructorDemo("Testing"); //calling one argument constructor in java }}

• Output:Inside no argument constructorInside one argument constructor in Java with name: Testing

If this presentation helped you, please visit our page facebook.com/baabtra and like it.

Thanks in advance.

www.baabtra.com | www.massbaab.com |www.baabte.com

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: info@baabtra.com

top related