2 The Basics of OOP.pdf

Full Transcript

Course Packet 2 The Basic of Object- Oriented Programming Maria Lolita G. Masangcap Introduction This course packet will allow the students to explain the importance of the features of object-oriented programming which are encapsulation, abstraction, inheritance, and polymorphis...

Course Packet 2 The Basic of Object- Oriented Programming Maria Lolita G. Masangcap Introduction This course packet will allow the students to explain the importance of the features of object-oriented programming which are encapsulation, abstraction, inheritance, and polymorphism. Also, students will have a review of the concept of classes, objects, and methods. At the end of the course packet, students are expected to implement an algorithm using the features of OOP. Introduction At the end of this course packet, you will be able to explain and implement the importance of the concept of encapsulation, abstraction, and information hiding in programming as well as classes and methods Object Oriented Programming OOP is said to model the characteristics and behavior of the real world. It is considered as a programming language which is used to build application and software. OOP permits to break down a problem into smaller parts which is taken as objects and methods. Objects in OOP An object is described as the basic unit of OOP. Model: xPander 2019 Brand: Mitsubishi Color: Titanium Gray numPassenger: 7 In the real world, an object has Object, in OOP, is characterized as distinct characteristics and can a data field that has unique also perform various tasks. attributes and behaviors. Class in OOP A class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). Objects having the same characteristics and functions are joined to build a class; thus, an instance of a class is composed of a set of distinct objects. Basic Features of OOP Abstraction Encapsulation Inheritance Polymorphism Retrieved from https://allprowebdesigns.com/ Abstraction The process of hiding the complexities and presenting only the essential details of the system to the user. Its main concept is to show the needed properties of an object and disregarding the unnecessary details. Abstraction Abstract classes may or may not contain abstract methods, i.e., methods without body. But, if a class has at least one abstract method, then the class must be declared abstract. If a class is declared abstract, it cannot be instantiated. To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it. If you inherit an abstract class, you have to provide implementations to all the abstract methods in it. Abstraction Encapsulation It refers to the concept of binding Every class in java is variables and considered as an methods together implementation of which prevent them the concept of from being accessed encapsulation. by other classes. Retrieved from https://www.scientecheasy.com Encapsulation A programming technique that binds the class members together and prevents them from being accessed by other classes Field is declared private in the class to prevent other classes from accessing them directly Real world examples School bag Logging in to an email Bank Account Encapsulation Encapsulation The whole idea behind encapsulation is to hide the implementation details from users. If a data member is private, it means it can only be accessed within the same class. No outside class can access private data member (variable) of other class. By providing only a setter or getter method, you can make the class read-only or write-only. Encapsulation Encapsulation It is a way to achieve data hiding in Java The encapsulate It provides you because other class is easy to the control over class will not be test. So, it is the data. able to access the better for unit data through the testing. private data members. Abstraction vs. Encapsulation Abstraction solves the problem in the design side while encapsulation is the implementation. Abstraction hides complexity of the system by Encapsulation is all about bundling of related data and presenting to the user a simplified interface. behavior together while restricting/controlling access to them, and in the process partially or fully hiding these members. Inheritance The process by which one class acquires the properties(data members) and functionalities(methods) of another class The aim of inheritance is to provide the reusability of code so that a class has to write only the unique features and rest of the common properties and functionalities can be extended from another class. Parent Class (The class whose properties and functionalities are used(inherited) by another class is known as parent class, super class) Child Class (The class that extends the features of another class is known as child class, sub class or derived class.) Retrieved from http://www.btechsmartclass.com/java Inheritance Once a method is defined in the parent (super) class, that method is automatically inherited by all child classes (subclasses). Polymorphism Polymorphism means ability to take more than one form that an operation can exhibit In simple words, it different behavior at different means "Many Forms". instance depending upon the data passed in the operation. Polymorphism Parent Class Child Classes SquareClass PolygonClass CircleClass The main purpose of the render() method is to render the shape. However, the process of rendering a square is different than the process of rendering a circle. Hence, the render() method behaves differently in different classes. The render() is polymorphic. Real Life Examples of the Concept of OOP God is an OOP programmer!!! Real Life Examples of the Concept of OOP Class – Human Real Life Examples of the Concept of OOP Inheritance – Man and Woman Real Life Examples of the Concept of OOP Abstraction – Man and Woman can talk Real Life Examples of the Concept of OOP Encapsulation – Attributes and Behaviour of a Human Real Life Examples of the Concept of OOP Object Joy and Ethan Real Life Examples of the Concept of OOP Polymorphism – Human can act differently based on an input. Classes in Java Object-Oriented class MyPuppy Programming (OOP) was name conceived to model real- breed Attributes age world objects. Color In OOP, a class defines the barking() common variables hungry() Behavior sleeping() (attributes) and methods (functions) that are shared by all its objects. Classes in Java public class Puppy{ String name; String breed; Property or Variable int age; String color; void barking(){ } void hungry(){ Behavior or Method } void sleeping(){ } } A class is a blueprint from which individual objects are created. Object Object is the basic entity of class MyPuppy object PuppyKo object-oriented name “Tiger” programming language. breed “Pomeranian” Class itself does nothing age 3 color black but the real functionality is barking() barking() achieved through their hungry() hungry() objects. sleeping() sleeping() Object is an instance of the class. It takes the properties (variables) and uses the behavior (methods) defined in the class. Creating an Object Basically 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. Creating an Object To create an instance of a class or object, you need to use constructor and the new keyword. MyPuppy PuppyKo = new MyPuppy(); Class Name new operator ObjectName Call of a constructor (Instance of the class MyPuppy) Note: When an object is declared, it will contain all attributes that your class has and can use all the methods of the class. Constructors When discussing about classes one of the most important sub topic would be constructors. Every class has a constructor. If we do not explicitly write a constructor for a class, the java compiler builds a default constructor for that class. Each time a new object is created at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor. public class Puppy{ public Puppy(){ //constructor 1 // This constructor doesn’t have a parameter (default) } public Puppy(String name){ //constructor 2 // This constructor has one parameter, name. } } Constructor and New Keyword Every class has at least one of its The new keyword is used to create own constructor. an instance of a class Constructor creates an instance for It allocates space for a new the class. instance and calls the class Constructor initiates (initialize) constructor which initializes the something related to the class' variables of the object. methods. Constructor is the method which name is same to the class. But there are many difference between the method (function) and the Constructor. Creating Methods Methods Types of Methods a collection of code or a set of Predefined Methods – Readily statements grouped together to do available in the standard library a specific operation or task. for use (Eg. sqrt() and print()) written once but can be used User-Defined Methods – defined several times during the execution by developers or programmers to of the program perform a specific task Creating Methods Example of Methods public String getOwnerName() throws IOException{ String name; System.out.print("\nWhat is your name? "); name = input.readLine(); return name; } public void setAge(int age, String name){ puppyAge = age; System.out.print(name+" owns a pet and its age is "+puppyAge+"."); } Parts of a Method Access_specifier method_type method_name (parameter_list) { public String getOwnerName() throws IOException{ variable declaration; String name; System.out.print("\nWhat is your name? "); ……….. name = input.readLine(); return name; ……….. } return expression; public void setAge(int age, String name){ puppyAge = age; } System.out.print(name+" owns a pet and its age is "+puppyAge+"."); } Defining a Method method_type method_name (parameter_list) { method_typemethod_name is any data type for the parameter_list is any valid is aidentifier list of parameters separated variable declaration; returning value by commas ……….. variable declaration is a list of variable declared ……….. within the function return Expression; } return expression is a keyword is the value returned by the function Making Method Call When a method is called, the program temporarily leaves the current method to execute the called method. When the called method terminates, program control reverts to the calling method. Making Method Call ⚫ In order to interact with one another, the method passed arguments. ⚫ The calling or invoking method passes arguments (such as values contained in a variable and / or constant) to the called method. Making Method Call The called method receives the values passed to it and stores them in its parameters. The number and type of arguments passed by the calling method must agree with the number and type of the parameters in the called method. Classes in Java A class can contain any of the following variable types. ▪ Local variables - variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed. ▪ Instance variables - variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class. ▪ Class variables - Class variables are variables declared within a class, outside any method, with the static keyword. Accessing Instance Variables and Methods Instance variables and methods are accessed via created objects. To access an instance variable the fully qualified path should be as follows: ObjectName = new Constructor(); ObjectName.variableName; ObjectName.MethodName(); Q&A Any Questions??? CP2 The Basics of OOP

Use Quizgecko on...
Browser
Browser