OOP Android Club 2015
DO NOT REMEMBER! TRY TO UNDERSTAND!
Agenda Class Object Encapsulation Abstraction Inheritance Polymorphism
WHAT is OOP? Object-oriented programming (OOP) is a programming language model organized around objects rather than "actions" and data rather than logic.objects
Procedural programming
OOP
WHY to use OOP? Easy to design Easy to maintain Reusable code
HOW? Create Class Create Object of class
Class a class is an extensible program-code- template Includes: Variables Methods
Object Instance of class Экземпляр
Class example
Object example
Class iPhone: example public class Iphone { public static int capacity; public static String color; public void turnOn(){ System.out.println("Turning on"); }
Object iphone: example Iphone firstIphone = new Iphone(); firstIphone.capacity = 16; firstIphone.color = "White"; Iphone secondIphone = new Iphone(); firstIphone.capacity = 32; firstIphone.color = "Black";
Object iphone: practice Create new iphone object: thirdIphone Capacity: 128 Color: Gold Turn On
Class example
Object example
Create class: practice Create class: Android Create following Strings name head body rightHand leftHand rightLeg leftLeg
Class methods: practice Create method: jump Access modifier: public Returns: nothing Does: Print “Jumping android”
Class methods: practice 2 Create another method: dance Access modifier: public Returns: nothing Does: Print “Dancing like robot”
Object: practice ANVAR ANDREW ANDRUXA
Encapsulation Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods.
Инкапсуляция Инкапсуляция (по-русски: «сокрытие») — это свойство объектов скрывать некоторые свои данные и способы их обработки (методы) от окружающей его цифровой среды и, в частности, от кривых ручонок малоопытных программистов, оставляя «снаружи» только необходимые и/или требуемые свойства и функциональные возможности.
Do not show process
Encapsulation: Hide process
Encapsulation: example int day; int hour; int minute; int second; public void setDay(int d) { this.day = d; } public int getDay() { return day; }
Encapsulation: practice Create getter&setter for hour Create getter&setter for minute Create getter&setter for second
Encapsulation: create object Clock clock = new Clock(); clock.setDay(17);
Create object: practice Set hour Set minute Set second
Create object: practice2 Create another Clock object: clock2 Set day: 18 Hour: 0 Minute:5 Second: 45
Create object: practice3 Create another Clock object: clock3 Set day: 17 Hour: 15 Minute: 5 Second: 45
Create object: constructor Constructor – method where we initialize our class Clock clock = new Clock();
Empty constructor: example public Clock(){ }
Constructor: example public Clock(){ day = 17; hour = 20; minute = 5; second = 45; }
Constructor: practice Create constructor inside Android class Default values: Head: “white” Body: “white” Right hand: “white” Left hand: “white” Right leg: “white” Left leg: “white”
Constructor: practice2 Create constructor for Iphone class Capacity: 16 Color: “Black”
Setting values with constructor public Clock(int d, int h, int m, int s){ day = d; hour = h; minute = m; second = s; }
Setting values: practice Open class: Iphone Create constructor Using this constructor, create object: Iphone object Pass values Capacity: 16 Color: “White”
Setting values: practice2 Open class: Android Create constructor Using this constructor, create object: andrey Pass values: Name: Andrey Set colors for body parts
SUPER PRACTICE Create Class: Book Variables: title, author, isbn Methods: getters & setters Create constructor Create one Book object, set values using setters Create another Book object, set values using constructor
Project Euler
Questions? Any questions?
Review Lynda.com – Chapter 9. Creating Custom Classes
Homework
Thank you Thank you for your attention!