Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming

Similar presentations


Presentation on theme: "Object-Oriented Programming"— Presentation transcript:

1 Object-Oriented Programming
การโปรแกรมเชิงวัตถุด้วยภาษา JAVA Object-Oriented Programming มหาวิทยาลัยเนชั่น บุรินทร์ รุจจนพันธุ์ . ปรับปรุง 24 สิงหาคม

2 โปรแกรมภาษาจาวา class human {
public static void main (String args[]) { System.out.println(“wow”); }

3 หลักพื้นฐานของ OO in Java
1. Inheritance 2. Encapsulation 3. Polymorphism 3.1 Overloading 3.2 Overriding

4 Inheritance class father { void homeaddr(){System.out.println(“lp”);}
class child extends father { public static void main (String args[]) { homeaddr();

5 Encapsulation class friend { private int val;
public int getter(){ return this.val;} public void setter(int a){ this.val=a;} } class child { public static void main (String args[]) { friend x = new friend(); x.setter(5); System.out.println(x.getter());

6 Polymorphism : Overloading
overloaded methods: - appear in the same class or a subclass - have the same name but, - have different parameter lists, and, - can have different return type

7 Polymorphism : Overloading
class child { static int bag(){ return 1; } static int bag(int a){ return a; } static int bag(String a){ return a.length(); } public static void main (String args[]) { System.out.println(bag()); System.out.println(bag(5)); System.out.println(bag(“abc”)); }

8 Polymorphism : Overriding
overriding methods: - appear in subclasses have the same name as a superclass method - have the same parameter list as a superclass method - have the same return type as as a superclass method - the access modifier for the overriding method may not be more restrictive than the access modifier of the superclass method if the superclass method is public, the overriding method must be public if the superclass method is protected, the overriding method may be protected or public if the superclass method is package, the overriding method may be packagage, protected, or public if the superclass methods is private, it is not inherited and overriding is not an issue - the throws clause of the overriding method may only include exceptions that can be thrown by the superclass method, including it's subclasses

9 Polymorphism : Overriding (1/2)
class father { static void addr(){System.out.println(“lp”);} } class child extends father { static void addr(){System.out.println(“cm”);} public static void main (String args[]) { addr(); new father().addr();

10 Polymorphism : Overriding (2/2)
class father { void addr(){System.out.println(“lp”);} } class child extends father { void addr(){System.out.println(“cm”);} public static void main (String args[]) { child c = new child(); c.addr();


Download ppt "Object-Oriented Programming"

Similar presentations


Ads by Google