Download presentation
Presentation is loading. Please wait.
1
Objects as a programming concept
4
Topic D.1.1 Outline the general nature of an object
5
Definition: object An object is a ‘thing’.
An object is an abstract entity. It has two components: data and actions.
7
Object & Class Object - Objects have states (variables) and behaviors (methods). Example: A dog has states - color, name, breed as well as behaviors -wagging, barking, eating. An object is an instance of a class. Class - A class can be defined as a template/blue print that describes the behaviors/states that object of its type support.
8
Real world vs Software What are real-world objects? Cars, Dogs, Humans, etc. All these objects have a state and behavior. If we consider a dog, then its state is - name, breed, color, and the behavior is - barking, wagging, running Software objects also have a state and behavior. A software object's state is stored in fields and behavior is shown via methods.
9
Example: Fields States Methods Behaviours
10
Constructors Every class has a constructor.
Each time a new object is created, at least one constructor will be invoked (called/run). The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor.
11
Steps in object creation:
A class provides the blueprints for objects. An object is created from a class. In Java, the new key word is used to create new objects. There are three steps when creating an object from a class: Declaration: A variable declaration with a variable name with an object type. Instantiation: The 'new' key word is used to create the object. Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
12
Example: Output:
13
Summary of code
14
Output:
15
We tend to split classes
Many methods Object Class main() Running Class
16
Output:
17
Summary – key terms Think Pair Share Object Class State Behaviour
Field Method Constructor Declaration Instantiation Initialization
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.