Download presentation
Presentation is loading. Please wait.
Published byMeghan Quinn Modified over 9 years ago
2
Introduction to Objects What is a constructor?
3
Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();
4
Let’s start simple A circle
5
What are some of the attributes of a circle? Radius (most obvious) Color Border Position
6
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
7
Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; }
8
Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } Heading for the class
9
Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } A property of each circle
10
Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } A method named Area that will calculate the area of that specific circle
11
Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } A constructor Used to initialize the circle Let’s see how in the next slide -Name same as the class -No type
12
Creating circles Circle circle1 = new Circle(10); Radius:10 circle1
13
Creating circles Circle circle1 = new Circle(10); radius:10 circle1 Circle circle2 = new Circle(15); radius:15 circle2
14
What’s the difference? Circle circle1 = new Circle(10); Circle circle1; Creates a REFERENCE Like having a telephone number for a friend.. a means to find them. But this one is a contact without a number.
15
Creation requires a new new uses the constructor
16
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 constructor This constructor chooses a radius (1) for us.
17
Creating circles Circle circle1 = new Circle(); radius:1 circle1 Circle circle2 = new Circle(15); radius:15 circle2 default constructor
18
Circle() : more constructors class 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; } }
19
Circle() : more constructors class 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();
20
Circle() : more constructors class 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);
21
Circle() : more constructors class 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);
22
As many constructors as you want!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.