Objects and Classes Jericho Jover V. Magpantay Department of Information Technology Cavite State University – Trece Campus TOPICS • • • • • ITEC 106 Objects and Classes What is Object in Java? Characteristics of Object What is a Class in Java? Instance Variable in Java Objects and Classes In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior. For example, the animal type Dog is a class while a particular dog named Tommy is an object of the Dog class. ITEC 106 Java Classes A class in Java is a set of objects which shares common characteristics/ behavior and common properties/ attributes. It is a user-defined blueprint or prototype from which objects are created. For example, Student is a class while a particular student named Ravi is an object. ITEC 106 Properties of Java Classes 1. 2. 3. 4. Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created. Class does not occupy memory. Class is a group of variables of different data types and a group of methods. A Class in Java can contain: • • • • • ITEC 106 Data member Method Constructor Nested Class Interface Class Declaration in Java ITEC 106 Example of Java Class ITEC 106 Components of Java Classes In general, class declarations can include these components, in order: 1. 2. 3. 4. 5. 6. ITEC 106 Modifiers : A class can be public or has default access. Class keyword: class keyword is used to create a class. Class name: The name should begin with an initial letter (capitalized by convention). Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent. Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface. Body: The class body is surrounded by braces, { }. Java Objects An object in Java is a basic unit of Object-Oriented Programming and represents real-life entities. Objects are the instances of a class that are created to use the attributes and methods of a class. A typical Java program creates many objects, which as you know, interact by invoking methods. An object consists of : 1. State : It is represented by attributes of an object. It also reflects the properties of an object. 2. 3. ITEC 106 Behavior : It is represented by the methods of an object. It also reflects the response of an object with other objects. Identity : It gives a unique name to an object and enables one object to interact with other objects. Java Objects : Example Note: When we create an object which is a non primitive data type, it’s always allocated on the heap memory. ITEC 106 Declaring Objects (Also called instantiating a class) When an object of a class is created, the class is said to be instantiated . All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances. ITEC 106 Declaring Objects : Example As we declare variables like (type name;). This notifies the compiler that we will use the name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. So for reference variables , the type must be strictly a concrete class name. In general, we can’t create objects of an abstract class or an interface. ITEC 106 Initializing a Java object The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor. ITEC 106 Initializing a Java object: Example ITEC 106 Initializing a Java object: Example This class contains a single constructor. We can recognize a constructor because its declaration uses the same name as the class and it has no return type. The Java compiler differentiates the constructors based on the number and the type of the arguments. The constructor in the Student class takes four arguments. The following statement provides “Stephen Navarro”, “General Trias, Cavite”,35, and “2015-100-450” as values for those arguments: An Address ITEC 106 Name: Stephen Navarro Address: General Trias, Cavite Age: 25 Student Number: 2015-100-450 Initialize by using method/function: Example ITEC 106 Initializing a Java object Note: All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, also called the default constructor. This default constructor calls the class parent’s no-argument constructor (as it contains only one statement i.e super();), or the Object class constructor if the class has no other parent (as the Object class is the parent of all classes either directly or indirectly). ITEC 106 Ways to Create an Object of a Class There are four ways to create objects in Java. Strictly speaking, there is only one way (by using a new keyword), and the rest internally use a new keyword. ITEC 106 Ways to Create an Object of a Class 1. Using new keyword - It is the most common and general way to create an object in Java. ITEC 106 Ways to Create an Object of a Class 3. Using clone() method - clone() method is present in the Object class. It creates and returns a copy of the object. ITEC 106 Ways to Create an Object of a Class 4. Deserialization - De-serialization is a technique of reading an object from the saved state in a file. ITEC 106 Instance variable as final in Java The instance variable is declared inside a class but not within any method, constructor, block etc. If we don’t initialize an instance variable, then JVM automatically provide default value according to the data type of that instance variable. But if we declare an instance variable as final, then we must have to take care about the behavior of instance variable. • There is no requirement as such to make the variable final. However, when it is true that you explicitly intend to never change the variable, it is often good practice to make it final • We have to use instance variable as final in the creation of immutable class. ITEC 106 Important points about final instance variable: 1. Initialization of variable Mandatory : If the instance variable declared as final, then we have to perform initialization explicitly whether we are using it or not and JVM won’t provide any default value for the final instance variable. ITEC 106 Important points about final instance variable: 2. Initialization before constructor completion : For final instance variable we have to perform initialization before constructor completion. We can initialize a final instance variable at the time of declaration. ITEC 106 Important points about final instance variable: 3. Initialize inside a non-static or instance block : We can also initialize a final instance variable inside a non-static or instance block also. ITEC 106 Important points about final instance variable: 4. Initialization in default constructor : Inside default constructor we can also initialize a final instance variable ITEC 106 Objects and Classes : Exercise 1. Create a Java project named Objects_and_Classes. ITEC 106 Objects and Classes : Exercise 2. Create a class Car with attributes and methods that simulate real-life characteristics of a car. ITEC 106 Objects and Classes : Exercise 2. Create a class Car with attributes and methods that simulate real-life characteristics of a car. ITEC 106 Objects and Classes : Exercise 3. Class Definition: A class is like a blueprint for creating objects. The class Car will define the attributes (such as color, brand, and speed) and methods (such as drive and brake) of the car. 4. Attributes: These are characteristics of the car. In Java, attributes are defined as variables inside the class. For example, color, brand, and speed. 5. Methods: These are actions that a car can perform. Methods like drive, brake, and honk will be defined inside the class ITEC 106 Objects and Classes : Exercise 6. Main Class • • • The user will input the color, brand and speed. Main Class: Car is the main class in this program because it contains the main method. main Method: This is where the program begins execution. In the main method: • • • ITEC 106 We create an object myCar of the Car class. We use methods like drive(), brake(), and honk() to interact with the car object. Execution: When you run the program, the JVM starts at the main method, executing the code in order, which tests the functionality of the Car class. Objects and Classes : Exercise 6. Output ITEC 106