methods and constructors in java

15

Click here to load reader

Category:

Education


4 download

DESCRIPTION

Methods and constructors in java

TRANSCRIPT

Page 1: Methods and constructors in java
Page 2: Methods and constructors in java

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

Page 3: Methods and constructors in java

Methods and constructors in java

Shanu k [email protected]/shanunnitwitter.com/shanuin.linkedin.com/in/shanu k k9656153432

Page 4: Methods and constructors in java

Week Target Achieved

1 25 25

2 27 30

3 35 29

Typing Speed

Page 5: Methods and constructors in java

Jobs Applied# Company Designation Applied Date Current Status

1

2

3

Page 6: Methods and constructors in java

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

Page 7: Methods and constructors in java

Method declaration

Type methodname(parameter_list){

Method_body;}

Page 8: Methods and constructors in java

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

Page 9: Methods and constructors in java

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);}

}

Page 10: Methods and constructors in java

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

Page 11: Methods and constructors in java

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

Page 12: Methods and constructors in java

• 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 }}

Page 13: Methods and constructors in java

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

Page 14: Methods and constructors in java

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

Page 15: Methods and constructors in java

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: [email protected]