ObjectOriented Programming with Java Mr Than Quang Minh
Object-Oriented Programming with Java Mr. Than Quang Minh thanqminh. com Course URL: /courses/oopjava Lecture 3: OOP 1
Today’s topic ² OOP Concepts ² Class, Object, Interface, Abstract Class Review ² Inheritance review in Java ² Examples 03/10/2020 Lecture 3: OOP 2
OOP Concepts ² https: //www. javatpoint. com/java-oops-concepts ² OOPs (Object-Oriented Programming System) ² Object means a real-world entity such as a pen, chair, table, computer, watch, etc. ² Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies software development and maintenance by providing some concepts: § § § Object Class Inheritance Polymorphism Abstraction Encapsulation 03/10/2020 Lecture 3: OOP 3
OOP Concepts (cont. ) ² Apart from these concepts, there are some other terms which are used in Object-Oriented design: § § § Coupling Cohesion Association Aggregation Composition 03/10/2020 Lecture 3: OOP 4
Class ² Class is template to create object (instance) ² field = instance variable = attribute = property = state ² method = function = operation = behavior ² sending a message to an object = calling a function ² These are all approximately true ² https: //www. javatpoint. com/object-and-class-in-java 03/10/2020 Lecture 3: OOP 5
Class & Instance examples public class Person { public String name; // public property private int weight; // private property protected int health; public void eat() { weight = weight + 1; increase. Health(); } public int get. Power() { return weight; } protected int increase. Health() { health = get. Health() + 1; } private int get. Health() { return health; } public int health 2() { return health; } } 03/10/2020 public class Person. Test { public static void main(String[] args) { Person Hieu = new Person(); Hieu. name = "Duong Viet Minh Hieu"; System. out. println(Hieu. name); System. out. println(Hieu. get. Power()); Hieu. eat(); System. out. println(Hieu. get. Power()); //Cannot call increase. Health() // Cannot get. Health() System. out. println(Hieu. health 2()); } } Lecture 3: OOP 6
Class & Instance ² Constructor: https: //www. javatpoint. com/java-constructor ² Instance variable & method ² Static variable & Method: https: //www. javatpoint. com/static-keyword-in-java 26/09/2020 Lecture 2: Java Basic 7
Scope ² Public = allow outside to see. ² Protected = not allow outside to see, but let my children & grant children see ² Private = only me can see. ² Package https: //www. dreamincode. net/forums/topic/158726 -scopeand-static-variables/ 03/10/2020 Lecture 3: OOP 8
Scope & Encapsulation ² Although we can expose object states to be accessed from outside, we are not recommended to do so. “expose = public: e. g. – public int age(); ² The reason is that we should always control our behaviors. For instance, instead of exposing age, we might define a method like this: public int get. Age() { if (the asking person is my friends) return age; else return 30; } ² We have 3 scopes of behaviors & methods: public, protected, private. 03/10/2020 Lecture 3: OOP 9
”this” keyword ² https: //www. javatpoint. com/this-keyword 03/10/2020 Lecture 3: OOP 10
Homework ² Read OOP slides (Chapter 1) ² OOP Exercise (1. 1 1. 9) 03/10/2020 Lecture 3: OOP 11
- Slides: 11