Published on July 9, 2014
PowerPoint Presentation: 1 OBJECT-ORIENTED PROGRAMMING CBOP3203 – Object-Oriented Programming Learning Outcomes: 2 Learning Outcomes By the end of this part, you should be able to: Explain the meaning of class and objects and their difference Explain the components that make a class Write class programs and create objects from the class Introduction: 3 Introduction The strength of Java is on the object oriented programming approach. Object-oriented programming (OOP) is a programming paradigm using " objects " – which is data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Object-oriented programming allows resuablity of codes, programs are easy to maintain and programs are more flexibible and expandable. In object- oriented programming , two important concepts that you should master are class and object. Definitions: 4 Definitions Class is the template or blueprint that enables you to create the objects. object is an instance of class. It means you create object from the class. PowerPoint Presentation: Objects are created from the class Difference between object and primitive data types: Difference between object and primitive data types Primitive type such as int , double and char (to name a few) is predefined by the language On the other hand, an object is a variable that you define through its state and Class: 7 Class In object oriented programming, the first thing that you need to do is to create the class program. Class consists of identity, attributes and methods to perform a specific task. Structure of class: Structure of class Class Example : Class Example Methods: Methods In Java, there are two types of methods that can be implemented in a class program They are constructor methods and members methods . Constructor Methods vs Members Methods: Constructor Methods vs Members Methods Class example with constructor and member methods : 12 Class example with constructor and member methods Constructor is optional: Constructor is optional Constructors are not compulsory in the class. In cases where the class does not have a constructor, the compiler automatically provides a no-argument, default constructor. Nevertheless, it will be an error for calling a no-argument constructor when creating an object if a class already has constructor with argument unless you have already defined your own no-argument constructor in the class. Problem : 14 Problem Write a class program for Student. This class has attributes student name, marks for test 1, marks for test 2 and the total marks. The class will have one constructor to receive student’s name via parameter, two member methods to read the marks for test 1 and test 2 respectively. These marks are captured through the parameter. It also has another two member methods to display the total marks and the grade respectively. [Formula to obtain the grade: If the total marks is between 80-100, the grade is A, if the total marks is between 50-79, the grade is B. If the total marks is less than 50, the grade is C] Solution (1/2): Solution (1/2) 15 Solution (2/2): Solution (2/2) 16 Creating objects : Creating objects In the solution, the class Student program cannot be executed because : (i) It is just a class definition (ii) The Student class program does not have the main method. If you want to use the class Student, you need to create the object of the class Student inside a main method. And if possible, do it in a new file. 17 Syntax for Object Creation: Syntax for Object Creation Calling the member methods and attributes: Calling the member methods and attributes Object can call the member methods and attributes using the following format: Problem: Problem Create two objects for the class Student presented earlier Solution: Solution 22 public, private keywords in a class: public , private keywords in a class Sometimes you might come across the keywords public or private have been declared during the attribute or method declarations. public methods or public attributes can be accessed from outside of its class. private methods or private attributes cannot be accessed from outside of its class. It means private methods and private attributes are confined in its own class. 23 Example of class using public, private keywords: Example of class using public , private keywords Summary: 25 Summary Java is an object-oriented programming language (OOP). Object-oriented programming allows resuablity of codes, programs are easy to maintain and programs are more flexibible and expandable. In object- oriented programming , two important concepts that you should master are class and object. Class is the template or blueprint that enables you to create the objects. Identity, attributes and methods are the components that make up a class.