Java Classes and Objects - Module 1 PDF

Summary

This module covers Java classes and objects as part of an object-oriented programming course. Topics include the difference between procedural and object-oriented programming, creating classes and objects, attributes, constructors, and method overloading. The course is taught by Engr. Bernardith Batoy-Cruz at NU East Ortigas.

Full Transcript

OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Module 1 Java Classes and Objects Course Facilitator: Engr. Bernardith Batoy-Cruz OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Objectives: After studying this module, students should be ab...

OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Module 1 Java Classes and Objects Course Facilitator: Engr. Bernardith Batoy-Cruz OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Objectives: After studying this module, students should be able to: Introduce you to objects, classes, inheritance, interfaces, and packages. Describe the difference between a class and an object. Be able to differentiate between the data and instructions part of an object definition. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Java Objects and Classes OBJECT ORIENTED PROGRAMMING (OOP-JAVA) OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Procedural vs OOP Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Procedural Programming OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Object Oriented Programming OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Object-oriented programming has several advantages over procedural programming: OOP is faster and easier to execute. OOP provides a clear structure for the programs. OOP helps to keep the Java code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug. OOP makes it possible to create full reusable applications with less code and shorter development time. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Programming languages designed primarily for OOP include: Java Python C++ Other programming languages that pair with OOP include: Visual Basic.NET PHP Javascript OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Structure of OOP Classes are user-defined data types that act as the blueprint for individual objects, attributes and methods. Objects are instances of a class created with specifically defined data. Objects can correspond to real-world objects or an abstract entity. When class is defined initially, the description is the only object that is defined. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Structure of OOP Methods are functions that are defined inside a class that describe the behaviors of an object. Each method contained in class definitions starts with a reference to an instance object. Additionally, the subroutines contained in an object are called instance methods. Attributes are defined in the class template and represent the state of an object. Objects will have data stored in the attributes field. Class attributes belong to the class itself. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) What are Classes and Objects? Classes and objects are the two main aspects of object-oriented programming. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) So, a class is a template for objects, and an object is an instance of a class. When the individual objects are created, they inherit all the variables and methods from the class. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Java Classes and Objects Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake. A Class is like an object constructor, or a "blueprint" for creating objects. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Create a Class To create a class, use the keyword class: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Create an Object In Java, an object is created from a class. We have already created the class named Main, so now we can use this to create objects. To create an object of Main, specify the class name, followed by the object name, and use the keyword new: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Example: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Multiple objects: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Java Class Attributes We used the term "variable" for x in the example (as shown below). It is actually an attribute of the class. Or you could say that class attributes are variables within a class OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Accessing Attributes You can access attributes by creating an object of the class, and by using the dot syntax (.): The following example will create an object of the Main class, with the name myObj. We use the x attribute on the object to print its value: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Modify Attributes You can also modify attribute values: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Or modify existing attribute values: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) If you don't want the ability to override existing values, declare the attribute as final: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Multiple Objects If you create multiple objects of one class, you can change the attribute values in one object, without affecting the attribute values in the other: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Multiple Attributes You can specify as many attributes as you want: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Java Constructor In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. Two types of Java constructors: Default constructor (no-arg constructor) Parameterized constructor Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn't have any. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) OBJECT ORIENTED PROGRAMMING (OOP-JAVA) OBJECT ORIENTED PROGRAMMING (OOP-JAVA) OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Java static keyword The static keyword in Java is used for memory management mainly. The static keyword belongs to the class than an instance of the class. The static can be: 1. Variable (also known as a class variable) 2. Method (also known as a class method) 3. Block 4. Nested class OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Java static variable Understanding the problem without static variable: OBJECT ORIENTED PROGRAMMING (OOP-JAVA) OBJECT ORIENTED PROGRAMMING (OOP-JAVA) OBJECT ORIENTED PROGRAMMING (OOP-JAVA) API Documentation: For online version: https://docs.oracle.com/javase/8/docs/api/ For offline version: Download and extract (save) on your local disk. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) Java Method Overloading With method overloading, multiple methods can have the same name with different parameters: Note: Multiple methods can have the same name as long as the number and/or type of parameters are different. OBJECT ORIENTED PROGRAMMING (OOP-JAVA) OBJECT ORIENTED PROGRAMMING (OOP-JAVA) OBJECT ORIENTED PROGRAMMING (OOP-JAVA) References: https://www.javatpoint.com/static-keyword-in- java https://www.tutorialspoint.com/java/index.html https://www.techtarget.com