🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

CCS1303_OOP_Lecture02_DatatypesDefiningClasses.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

DATA TYPES & DEFINING CLASSES CCS1303 OBJECT ORIENTED PROGRAMMING Lecture 02 Swapna Premasiri 1 RECAP Objects, Methods and Classes Objects may have diverse behavior Can easily specify objects using classes Java...

DATA TYPES & DEFINING CLASSES CCS1303 OBJECT ORIENTED PROGRAMMING Lecture 02 Swapna Premasiri 1 RECAP Objects, Methods and Classes Objects may have diverse behavior Can easily specify objects using classes Java also provides primitive values. Using these, (bigger) objects can be described / constructed 2 PRIMITIVE DATA TYPES byte, short, int, long Allows representation of discrete integer values These integer values may be 8, 16, 32, and 64 bits respectively float & double Allows representation of single and double precision floating points May have floating point values of 32 and 64 bits respectively char – 16 bit characters Boolean – True / False 3 ACTIVITY Can you try to give examples of when each of the primitive data types might be used? 4 DEFINING OBJECTS Objects are defined using class constructs Class Constructs in Java includes The keyword class Followed by class name For example: class Counter { attribute and method declarations } 5 DEFINING OBJECTS (CONTD.) The object’s attributes are: Primitive types used to represent the object OR Nested component objects 6 DEFINING OBJECTS (CONTD.) The object’s attributes are: Primitive types used to represent the object OR Nested component objects 7 DEFINING OBJECTS (CONTD.) When defining objects in Java, Data type precedes the variable name Like in the example, int precedes the variable name number Defining data types allows Values for a variable to be anticipated Therefore, storage is set aside for these values Knowing which operations are valid and applicable 8 DEFINING METHODS Methods definition in a class construct is made up of: 1. Method header 2. Code body (surrounded by braces) 9 ACTIVITY Inspect the code below. Are there any attributes, methods or classes? If so, identify them. class Counter { int number void add() { number = number + 1 } } 10 ACTIVITY Inspect the code below. Are there any attributes, methods or classes? If so, identify them. class Counter { int number attribute void add() { method number = number + 1 } } 11 DEFINING METHODS (CONTD.) In the code given, note the return type and the method name class Counter { int number attribute Return Type void add() { method number = number + 1 Method Name } } 12 DEFINING METHODS (CONTD.) A method can be invoked to correspond to a message E.g. add() in the above example can be invoked by a message add sent to the object The object may return a value to the sender of the message The type of the value being returned is the “Return Type” For example: If no result needs to be returned, void is used More methods can be added within the class 13 ACTIVITY If a message add was sent and the resulting integer, number, was to be returned, what would be the return type? 14 OBJECT INSTANTIATION A class construct is a template for objects to be created No instances are created without calling the object allocator function new() For example: new Counter() will return a new instance of the counter class For this new object to be referred to, it needs to be assigned to a variable For example: Counter carpark ; carpark = new Counter() ; 15 OBJECT INSTANTIATION (CONTD.) If there were more than one new object to be created using the counter template, For example, say counters were used at the entrance and exit as well Then in addition to carpark, Counter entrance, exit ; entrance = new Counter() ; exit = new Counter() ; 16 OBJECT ACCESSING & MESSAGE PASSING Attributes and Methods of an object are considered as it’s characteristics They can be accessed by the operator “. ” What is this operator called? With reference to the same example as above: Total number from all counters will be carpark.number + entrance.number + exit.number Each of the counters may be initialized individually too, as follows: carpark.initialize() entrance.initialize() 17 ACTIVITY In the example code given below, What is the class name ? Does the class have any attributes? Have any objects been instantiated? If so, how did you identify which line it was? public class PrintVal { int x = 10 ; public static void main(String args []) { PrintVal NewObj = new PrintVal (); System.out.println(NewObj.x); } } 18 THANK YOU Faculty of 19

Use Quizgecko on...
Browser
Browser