21
Introduction to Objects What is a constructor?

Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Embed Size (px)

Citation preview

Page 1: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Introduction to Objects

What is a constructor?

Page 2: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Use type to create a variable

Use class to create an object

int x;

Circle mycircle = new Circle();

Page 3: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Let’s start simple

A circle

Page 4: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

What are some of the attributes of a circle?

• Radius (most obvious)

• Color

• Border

• Position

Page 5: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Let’s start with a simple Circle class

• Just a radius– No borders or colors

• A means of asking it for it’s area.

• This will serve as the basis (a type or class) for creating lots of circles

Page 6: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Circle()

class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double this area = radius*radius*Math.PI; }

}

Page 7: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Circle()

class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double this area = radius*radius*Math.PI; }

}

Heading for the class

Page 8: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Circle()

class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double this area = radius*radius*Math.PI; }

}

A property ofeach circle

Page 9: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Circle()

class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double this area = radius*radius*Math.PI; }

}

A method named Areathat will calculate thearea of that specific circle

Page 10: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Circle()

class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double this area = radius*radius*Math.PI; }

}

A constructorUsed to initialize the circleLet’s see how in the next slide

-Name same as the class-No type

Page 11: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Creating circles

Circle circle1 = new Circle(10);

Radius:10

circle1

Page 12: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Creating circles

Circle circle1 = new Circle(10);

radius:10

circle1

Circle circle2 = new Circle(15);

radius:15

circle2

Page 13: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

What’s the difference?

• Circle circle1 = new Circle(10);

• Circle circle1; Creates a REFERENCELike having a telephone number for a friend.. a means to find them. Butthis one is a contact without a number.

Page 14: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Creation requires a new

new uses the constructor

Page 15: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Circle()a default constructor

class Circle {

double radius;

Circle() { radius = 1.0; }

Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; }

}

Another constructorThis constructor chooses a radius (1) for us.

Page 16: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Creating circles

Circle circle1 = new Circle();

radius:1

circle1

Circle circle2 = new Circle(15);

radius:15

circle2

default constructor

Page 17: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Circle() : more constructorsclass Circle { double radius,x,y;

Circle() { radius = 1.0; x=0.0; y=0.0; }

Circle(double r) { radius = r;

x=0.0; y=0.0; } Circle(double r,double xpos, double ypos ) { radius = r;

x=xpos; y=ypos; } }

Page 18: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Circle() : more constructorsclass Circle { double radius,x,y;

Circle() { radius = 1.0; x=0.0; y=0.0; }

Circle(double r) { radius = r;

x=0.0; y=0.0; } Circle(double r,double xpos, double ypos ) { radius = r;

x=xpos; y=ypos; } }

Circle c1 = new Circle();

Page 19: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Circle() : more constructorsclass Circle { double radius,x,y;

Circle() { radius = 1.0; x=0.0; y=0.0; }

Circle(double r) { radius = r;

x=0.0; y=0.0; } Circle(double r,double xpos, double ypos ) { radius = r;

x=xpos; y=ypos; } }

Circle c2 = new Circle(10.0);

Page 20: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Circle() : more constructorsclass Circle { double radius,x,y;

Circle() { radius = 1.0; x=0.0; y=0.0; }

Circle(double r) { radius = r;

x=0.0; y=0.0; } Circle(double r,double xpos, double ypos ) { radius = r;

x=xpos; y=ypos; } }

Circle c3 = new Circle(10.0,1.0,2.0);

Page 21: Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

As many constructors as you want!