Introduction to OOP in Java
OOP - Programs - Java -
Watcharin Puangplia : 9/11/ 11/53
Object-Oriented Programming ďœ‹ ďœŠ ďœŠ ďœ‹ ! " ŕš€ ďœ’ ďœŠ "% % ŕš€ &ďœ‹ ďœ‹ % '()) *+ďœŽ & ŕš‰ " ďœŠ ďœ‹ "% ) % & ďœŠ ) ďœŠ " ŕš€ ďœ‹ & ŕš‰ " ! ŕš€ ďœ’ ) & ŕš‰ . ŕš€ ďœŠ “ ŕš€0 ! (Object-Oriented Programming)â€? ) D E )F % ďœ‹ ! " ŕš€ ďœŠ “D E ŕš€0 ! (Object-Oriented Language)â€? ŕš€0ďœŠ C++, C#, Java ŕš€ ďœ’ ďœ‹
OOP Concept
Encapsulation Inheritance Polymorphism Abstraction
OOP Concept : Encapsulation ŕš€ ďœ’ )F .ďœŠ ) ŕš€ & &ďœ‹ W) ) ďœ‹ &ďœ‹ W)F ďœ‹D Class F ďœŠ ďœ‹(Wďœ‹ 0ďœ‹ŕš€ ŕš‡ ) ŕš€ % &ďœ‹ W) (Implement filed) Method (Wďœ‹ 0ďœ‹ ! 0ďœ‹ &ďœ‹ W) Class F ďœ‹(ďœŠ Method % F ďœ‹ . ŕš€ ďœ’ ŕš€ ŕš€&ďœ‹ & &ďœ‹ W) ďœŠ D D Class 0 ďœŽ& ) Encapsulation % [ ďœŠ ŕš€ ) ) ďœ‹ F &&ďœ‹ W ) ďœ‹ &ďœ‹ W ) D Class ! % F ďœ‹ F ďœŠ () ďœŠ ďœ‹ ! &ďœ‹ W) ďœŠ ďœ‹ (Encapsulate) F ďœ‹
OOP Concept : Encapsulation
Encapsulation ๏ F ๏ ๏ เน "เน ๏ D เน &๏ ! &๏ W)F ๏ ๏ เน " เน "เน ๏ ' Encapsulation . % ๏ (W๏ ๏ ) !"% + 0๏ ]๏ ) ๏ (. เน ๏ ) ) เน + F ๏ เน " ]๏ ) ๏ เน + (W๏ 0๏ F ๏ W๏ " ) F ๏ เน เน เน ๏ เน ) เน F ๏ ( ๏ ๏ F ๏ เน * ๏ % F ๏ % เน เน ") )๏ ) เน 0๏ & ! ๏ (W๏ & & W๏ + & ! !& ! ๏ F ๏ ๏ F ๏ "% เน ๏ ๏ W๏ ๏ เน ๏ ๏ ) 0 เน ๏ D ! ๏ % ๏ F ๏ F . เน เน .๏ ๏ F ๏ "% เน ๏ ๏ W๏ เน ๏ Encapsulation
OOP Concept : Encapsulation
๏ . เน เน % ) " เน & เน % (๏ !๏ เน เน ๏ (W ๏ ๏ เน "เน ๏ เน & เน % & เน เน " ๏ ๏ ]๏ ) ๏ ) เน + & เน "% เน * ๏ เน & เน % % F ๏ . ]๏ ) ๏ ) เน + เน )๏ เน . เน เน % ) " F ๏ W๏ เน ) ๏ W๏ เน 0๏ ]๏ ) ๏ เน เน "% เน % ) ๏ ) เน D เน ) W๏ เน ๏ ๏
Encapsulation : Example class EvalGrade String grade; method evaluate(double score) { /* algorithm to evaluate grade */ } method getGrade() { return grade; }
class StuGrade method main(String[] agrs) { EvalGrade grade = new EvalGrade(); grade.evaluate(48.5); String result = grade.getGrade(); System.out.println(result); }
OOP Concept : Inheritance ŕš€ ďœ’ )F % Class !W ďœ‹ ) 0ďœ‹ WďœŠ )ďœ‹ ďœ‹ ŕš€ ďœ’ Class ďœŠ (Sub class) 0ďœ‹ )F 0 " Class ŕš€ (Super class) ) ŕš€* ŕš€ 0 ďœ‹ŕš€ ďœ‹ 0ďœ‹ ďœŠ ŕš€* & ŕš‰ . Class ŕš€ ŕš‡ ! 0ďœ‹ F ďœ‹ŕš€ ŕš€ ďœŠ Class ďœŠ " F ďœ‹ 0 & Class ŕš€ * ďœ‹ 0 ŕš€* & ŕš‰ ŕš€ ) ) 0 WďœŠŕš€ ďœ‹ ďœŠ F " Super class 0 ďœŽ& Inheritance ) ŕš€ ) ďœ‹ Class ďœŠ ' Class ďœŠ ďœ‹ ďœ‹ ŕš‰ ' ) 0ďœ‹ )ďœ‹ Class ŕš€ WďœŠ )ďœ‹ ŕš‡ % Class WďœŠ )ďœ‹ ) 0ďœ‹ ďœŠ (Re-useable) F ďœ‹ ŕš€
OOP Concept : Inheritance
Inheritance เน ๏ ') E' ) *o !๏ " ๏ ๏ เน เน 0๏ ๏ & !เน ๏ F ๏ เน p๏ ๏ F ๏ )W & เน !เน ๏ F ๏ ) เน p๏ ๏ F ๏ เน 0๏
เน & D E Java เน เน F ๏ "% เน ๏ ๏ ๏ ) & เน ๏ เน เน ๏ เน ! 0๏ ) ๏ F ๏ เน เน ๏ F ๏ W๏ )๏ 0๏ ๏ เน ๏ 0 ๏ F ๏
OOP Concept : Inheritance ŕš€ ŕš€ Inheritance " ďœŠ ) ďœŠ ' ďœ‹ % Wďœ‹" ŕš‰ SuperClass ŕš€ ďœ’ ) ŕš€ ďœ’ ďœ‹ ďœ‹ ) ' SubClass ŕš€ ďœ’ ) ' " ) FinalClass ) F ďœŠ ďœ‹ ) ' Override method ŕš€ ) ' & ŕš€ + ) )W ďœ‹ ďœŠ " ) ďœŠ
OOP Concept : Inheritance
SuperClass ŕš€ ďœŠ ďœŠ ) ďœŠ ŕš€ ďœ’ ) ŕš€ ďœ’ ďœ‹ ďœ‹ ) ') E' ŕš€0ďœŠ ) Animal ŕš€ ďœ’ SuperClass & ) Lion, ) elephant, ) Tiger, ) Dog ) ) Cat SubClass ŕš€ ďœŠ ďœŠ ) )W ŕš€ ďœ’ ) ') E' ( ŕš‰ ďœŽ ) ŕš€ + )
Inheritance : Example class Person String name; method setName(String name) method getName()
class Employee extends Person double salary; String position, dept; method setSalary() method getSalary() method ‌
class Person String titleName, midName, lastName; method ‌
class Manager extends Employee double positionSal; method setPositionSal() method getPositionSal ()
OOP Concept : Polymorphism ŕš€ ďœ’ )F " Instance & Class ďœŠ ďœ‹ Method ŕš€ )ďœŠ ŕš€ ! " ďœ‹ Method ! Parameter ŕš€ ďœ’ Instance & Class ďœŠ F ďœ‹ ŕš€ F& ďœŠ Instance ŕš€&ďœ‹ ŕš‰ ďœ‹ ŕš€ ďœ’ Instance ŕš€ " Class Class ďœŠŕš€ Instance % ďœ‹ ŕš€ ďœ’ Parameter () ŕš€ & ŕš‰ " % Instance ďœŠ Class ŕš€ )ďœŠ ŕš‰" & ŕš‰ WďœŠ ďœŠ Instance ŕš‰ Implement Method D Class & ŕš€ F ďœ‹ ďœŠ F 0 ďœŽ& ) Polymorphism ďœŠ (Flexibility) ŕš€ 0ďœ‹ Method ! " " Instance & Class ďœŠ ďœ‹ Method ŕš€
OOP Concept : Polymorphism ๏ เน 0๏ !๏ เน ) Shape . เน ๏ ) ๏ & W ๏ F ๏ ๏ " เน ๏ เน ) เน ) ) Shape เน + Area . 0๏ * เน & W เน )๏
เน ๏ ) & เน 2 ) Triangle ) Rectangle . inherit ) Shape
* เน เน ๏ F ๏ ())*+๏ ๏
OOP Concept : Abstraction ŕš€ ďœ’ )F ďœ‹ Super class & ŕš‰ ŕš€* ŕš€* ďœ‹!W & (Extend) F ŕš€ ďœ’ Class " ďœŽŕš€* ďœ‹ Class )ďœŠ ŕš€ ďœ’ ďœŠ % ďœ‹ Class Extend F ŕš€ F& ďœŠ Sub class " 0ďœ‹ F ďœ‹ ŕš‰ ďœ‹ Implement abstract method Super class ďœ‹ ďœŠ " " % Sub class ŕš‰ F ďœ‹ ŕš€ ďœ’ Instance F ďœ‹ 0 ďœŽ& Abstraction ŕš€ ďœ’ )F ďœ‹ 0ďœ‹ Class ďœŠ Extend " Super class ŕš€ ďœ’ x ŕš€ ďœŠ ŕš‡ ďœ‹ Super class F ďœŠ"% ŕš€ ďœ’ ďœ‹ Implement method ďœŠ ďœŠ " !W Override ŕš€ !W Extend Class F 0ďœ‹
Polymorphism & Abstract : Example abstrac class Animal abstract method talk() class Dog extends Animal method talk() { z .. z ; } class Cat extends Animal method talk() { เ ..เ ; }
class AnimalTest method main() { Dog d = new Dog(); Cat c = new Cat(); makeItTalk(d); // z .. z makeItTalk(c); //เ ..เ
} method makeItTalk(Animal a) { a.talk(); }
Programs Computer Programs ŕš€ ďœŠ ďœŠ Software ŕš€ ďœ’ % ) ďœŠ % WďœŠ Computer ŕš€ ďœ’ User 0ďœ‹ ďœ‹ Computer % !ďœ‹ Computer |" Programs Software ŕš‡F ďœŠ ďœŠ F ŕš€ " ďœŠ ŕš€ )ďœŠ F ďœŠ ! )() &ďœ‹ W)F ďœ‹ Program !W ŕš€& & ŕš‰ ďœ‹ D E Programming Languages
Programming Languages
Machine Language Assembly Language High-Level Language
High-Level Language
COBOL (COmmon Business Oriented Language) FORTRAN (FORmula TRANslation) BASIC (Beginner All-purpose Symbolic Instructional Code) Pascal (named for Blaise Pascal) Ada (named for Ada Lovelace) C & C++ (an object-oriented language, based on C) Visual Basic (Basic-like visual language developed by Microsoft) Delphi (Pascal-like visual language developed by Borland) Java
Compiling Source Code
Program !W ŕš€& & ŕš‰ ďœ‹ D E W ŕš€ ďœŠ ďœŠ Source Program Source Code Computer " 0ďœ‹ Compiler ) (Translate) Source Program ďœ‹ŕš€ ďœ’ Machine Language . ())*+ďœŽ F ďœ‹" ŕš€ ďœŠ Object Program Object Program " !W ŕš€0 ďœŠ (Link) Library ďœŠ ďœŠ ďœ‹ % ďœ‹ ŕš€ ďœ’ Linker . F ďœ‹ ())*+ďœŽ ŕš€ ďœŠ Executable File " ŕš‰ " " ! % F 0ďœ‹ Computer F ďœ‹ Source File
Compiler
Object File
Linker
Executable File
Java ŕš€ ďœ‹ ďœ‚ .|.1990 " Pattrick Naughton ) " Sun Microsystems F ďœŠ ) ďœŠ ) ďœ‹ D E ďœŒ ŕš€* 0ďœ‹ 'ďœŽŕš€ *7 F ďœŽŕš€ ďœŠ “ ďœŒ â€? ďœŠ F ďœŠF ďœ‹ WďœŠ ) ďœŠ James Gosling " ŕš€ ) 0 ŕš€ ďœ’ D E " (Java) ŕš€* 0ďœ‹ Netscape Navigator ŕš€
" "ďœŠ 0 *‡ " (JDK1.0) .|.1996
Java ? ŕš€ ďœ’ D E W ŕš€& ) *‡ W ŕš€0 ! (Object Oriented Language) ŕš€ ďœ’ Technology 0ďœ‹ *‡ ! % F ďœ‹ ) Platform ( ďœŠ ŕš€0ďœŠ Windows, Solaris, Linux) F ďœŠ"% ŕš€ ďœ’ ďœ‹ ŕš€& ďœŠ compile ďœŠ % ďœ‹ ! 0ďœ‹ ŕš€ ŕš€ * ‡ )ďœ‹ Platform F 0ďœ‹ Platform F ďœ‹ ďœŠ ŕš€ ďœ’ D E ) E' Case Sensitive 0 (Identifier) ďœŠ 0ďœ‹ " * " '
ďœŠ ďœŠ E ŕš€)ŕš‡ ) [ďœŠ ďœ‹ ŕš€0ďœŠ name ) Name " ! ďœŠ ŕš€ ďœ’ 0 ) 0
Java Program
Java applications Servlets JSPs (Java Server Pages) EJBs (Enterprise Java Beans) Applets Midlets
Java Development Tools
Java Development Kit (JDK) Java Runtime Environment (JRE) Java Platform Integrated Development Environment (IDE) Text Editors
Java Development Kit (JDK) 0 & ŕš€ 0ďœ‹ * ‡ 0ďœ‹ D E Java . % ďœ‹ ďœŠ ŕš€0ďœŠ Compile, Run, Debug Java ďœŠ ) F ďœ‹ " ŕš‰ JDK " ďœ‹ Java compiler Compile D E Java ŕš€ ďœ’ % byte code F ďœ‹ ())*+ďœŽŕš€ ďœ’ F])ďœŽ *.class Java runtime (Java Virtual Machine) Interpret F])ďœŽ *.class ŕš€ ďœ’ % & CPU )ďœ‹ ďœ‹ CPU )() % ŕš‰ Core library (API) ŕš€ ! WF ďœ‹ Derectory ../jdk1.*.*/bin
JDK Versions
JDK JDK JDK JDK JDK JDK JDK
1.02 (1995) 1.1 (1996) 1.2 (1998) 1.3 (2000) 1.4 (2002) 1.5 (2004) a. k. a. JDK 5 or Java 5 1.6
JDK Editions
Java2 Standard Edition (J2SE) - J2SE can be used to develop client-side standalone applications or applets. Java2 Enterprise Edition (J2EE) - J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages. Java2 Micro Edition (J2ME). - J2ME can be used to develop applications for mobile devices such as cell phones.
JRE Java Platform Software 0 & ) 0ďœ‹ % Run Java . ďœ‹ Java Virtual Machine (JVM) Java Interpreter Java Application Program Interface (Java API) Runtime Library
ŕš‰ Program Java ŕš€& & ŕš‰ " % Platform ŕš‡F ďœ‹ JRE Java Platform % WďœŠ
JRE Java Platform
Java Program
Java API Java VM Hardware-Based Platform Hardware(Windows/Intel เ )
Java Platform
IDE Tools & Text Editors IDE Tools Integrated Development Environment ŕš€ ďœŽ % * ‡ ŕš‰ ŕš€&ďœ‹ F ďœ‹ ďœ‹ ŕš€0ďœŠ Editing, Compiling, Building, Debugging
Borland JBuilder NetBeans Open Source by Sun Sun ONE Studio by Sun MicroSystems Eclipse Open Source by IBM
Text Editors EditPlus, Notepad, Wordpad, Textpad
ďœ‹ Java
% ďœŠ ŕš€ ŕš‡ ŕš€ ŕš€. ďœŽF ďœ‹ ŕš€0ďœŠ Applet ) ) D W % F ďœ‹ ) Platform F ďœŠ ďœ‹ recompiled ŕš€ ďœ’ D E ŕš€0 ! ŕš€ ďœŠ )F & D E "% F ďœŠ ) F ďœŠ . .ďœ‹
Java Program Processing Compile--Time Compile
Run--Time Run
Hello, this is my first java program!
Display Result
Java Program Processing IDE Tools or Text Editors Create/Modify and Save
Java Compiler Compile Java Interpreter Interpret Evaluate Results
JVM
Error
Error
CPU Error
Error Type
Error & Program 3 ŕš€D Compile-Time Errors Compiler ŕš€" Syntax Error &' Compile Run-Time Errors * Error &' Program % ŕš€0ďœŠ ())*+ďœŽ& "% 10/0 Logical Errors Program % ďœŠ())*+ďœŽ F ďœ‹F ďœŠ!W ďœ‹ "ŕš€ ďœ’ ŕš€* 0ďœ‹ W % ' ( ŕš€ )% % & % (
Java Program Structure
Comments Reserved words Modifiers Statements Blocks Classes Methods The main method
Comment &ďœ‹ ŕš€&ďœ‹ F ŕš€* 0ďœŠ + ďœ‹ & ďœŠ ďœ‹(Wďœ‹ ďœŠ !ŕš€&ďœ‹ " F ďœ‹ ďœŠ & ŕš‰ ŕš€* &ďœ‹ ŕš€ &ďœ‹ ŕš‰ &ďœ‹ ŕš€ ďœ’ Comment " F ďœŠ () ďœŠ % & ďœŠ F ŕš‡ Comment ! ŕš€ ďœ’ ďœŠ & D E " ďœ‹ ÂŒŕš€ 'Â?ďœŽ ŕš€& D E Java Comment 3 W ŕš‰ 1. 2. 3.
// This is comment /* This is comment */ /** This is comment */ /** This is comment **/
Reserved words % !W % F ďœ‹ )ďœ‹ D E Java &ďœ‹ % ďœŠ ďœ‹ % F 0ďœ‹ ŕš€0ďœŠ ŕš€ ďœ’ 0
0 Class )ďœŠ ŕš€ ďœ’ % ŕš€ÂŽ* % Compiler ďœ?"" Reserved Words ŕš‰ 50 % ŕš‰ abstract, assert, boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, enum, extends, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, try, void, volatile, while
Modifiers ŕš€ ďœ’ 0ďœ‹ % ' & &ďœ‹ W) (Data) , % (Method) ) Class ďœŠ % ďœŠ F & ŕš€& ŕš€&ďœ‹ ! ŕš€ ďœ’ ďœŠ F Modifier D E Java " 0ďœ‹ ) ďœŠ % Reserved Words ďœŠ Modifier ŕš€0ďœŠ public, private, protected, final, abstract
Statements ŕš€ ďœ’
& % )% % statement statement D E Java ďœ‹ ) ďœ‹ ; (Semicolon) ŕš€ ďœŠ statement D E Java System.out.println(“Welcome to Java!â€?);
Blocks ŕš€ ďœ’ 0ďœ‹ % & ŕš€& % & Class, Method
) ) ďœŠ & % Blocks " ŕš€ ďœ‹ ďœ‹ { ) ) ďœ‹ ďœ‹ } ŕš€ Class Class ďœ‹ Class block ) Method Method ďœ‹ Method block public class Hello { public static void main(String[] args) { Class block System.out.println Method block (“Hello, my name is Chavanitâ€?); } }
๏ ! "# $%& % OOP
) (Class) เน "เน ๏ (Object) เน + (Method)
Classes
เน ๏ ๏ ) "% เน ๏ ๏ Java เน ๏ 0๏ ๏ ๏ &๏ W) (Data) ) % (Method) เน & Java " ๏ Class ๏ ๏ 1 class " " ! % F ๏ ) เน เน เน ๏ * *๏ เน & ๏ เน "เน ๏ ๏ & เน เน "เน ๏ ๏ " ) เน " & ' เน , ! เน ๏ "
๏ ๏ ๏ & ' ๏
Object
เน "เน ๏ เน ๏ ๏ % W๏ " เน " เน &
OOP เน * ๏ OOP เน " เน ๏ เน "เน ๏ เน 0๏ เน "เน ๏ ! ๏ Honda, เน "เน ๏ ! ๏ Toyota เน ๏ ๏ เน ๏ เน "เน ๏ " ๏ เน เน 0 ( ) ๏ เน ๏ ' เน ย * (Attribute) ) ! % (Method) & เน "เน ๏ เน
Methods
เน + (Method) ! % & เน "เน ๏ ๏ ) เน 0๏ D ) ! ๏ เน "เน ๏ " เน + ๏ เน 0๏ เน , , ! เน ๏ ๏
Methods ŕš€ ďœ’ ďœŠ & F ďœ‹ 0ďœ‹ ďœ‹ ďœ?[ % ďœŠ ďœŠ Method " ďœ‹ statement ) ďœŠ & statement % )% ŕš‰ ďœŠ ďœ‹ " " Method ŕš‰ ! ďœ‹ & ŕš‰ ŕš€ ŕš€ ďœ’ Method & Object WďœŠ )ďœ‹ D E ŕš‡F ďœ‹ ŕš€0ďœŠ
Method Std output Object 0% Java 456 ď&#x153;&#x2019;% Library 6: ;<> ď&#x153;&#x160; ?ď&#x153;&#x2039;@
public class Hello { public static void main(String[] args) { System.out.println (â&#x20AC;&#x153;Hello, my name is Chavanitâ&#x20AC;?); } Method 6A ď&#x153;&#x2039; 5$% }
The main method ŕš&#x20AC; ď&#x153;&#x2019; Java Application ď&#x153;&#x2039; Main method Wď&#x153;&#x160; Class Class . ŕš&#x20AC; ď&#x153;&#x2019; Class 0ď&#x153;&#x2039;ŕš&#x20AC; % (Main class) JVM " Execute ď&#x153;&#x2039; ŕš&#x20AC; Main method ď&#x153;&#x2039; % ŕš&#x20AC; ď&#x153;&#x2019; )% public class Hello {
}
public static void main(String[] args) { System.out.println (â&#x20AC;&#x153;Hello, my name is Chavanitâ&#x20AC;?); } Main Method
Getting Started Java Program //Hello.java public class Hello { public static void main(String[] args) { System.out.println(â&#x20AC;&#x153;Hello, my name is Chavanitâ&#x20AC;?); } } Compile Run
javac Hello.java java Hello
By EditPlus By CMD
Java Program Compiling & Running