You are on page 1of 36

IADCS Diploma Course Object Oriented Programming

U Nyein Oo COO/Director(IT) Myanma Computer Co., Ltd(MCC)

OOP?
Software Development Technique  Effective use of programmer productivity  Way of Creating Large Scale Systems  Approaching 100% solution


Copyright: MCC

Advanced Java Programming

Basic Concepts
Object  Class  Inheritance  Polymorphism  Encapsulation  Data Abstraction

Copyright: MCC Advanced Java Programming

Object
A Person, Place or Concepts  Composed of Characteristics (data) and Behavior (Operation)  Can operate directly on its data  Interact sending message to each other


Copyright: MCC

Advanced Java Programming

Class
Structure that contains common data and function as entity for objects  Each object is an instance of a class


Copyright: MCC

Advanced Java Programming

Inheritance


The mechanism that permits a class to share the attributes and operations defined in one or more classes

Copyright: MCC

Advanced Java Programming

Inheritance(cont)


Subclass

The class which inherits from another class

Super class

The class from which another class inherits its behavior

Multiple Inheritance

A subclass inherits from two or more classes

Copyright: MCC

Advanced Java Programming

Polymorphism


The property that allows an operation to have different behavior on different objects

Copyright: MCC

Advanced Java Programming

Encapsulation


The process of binding data and methods together is known as encapsulation

Copyright: MCC

Advanced Java Programming

Data Abstraction
The process of identifying and grouping attributes and actions related to a particular entity as relevant to the application a hand.  Advantages - It focus on the problem - It identifies the essential characteristics and action - It helps the eliminate unnecessary details.

Copyright: MCC Advanced Java Programming

10

Access Specifiers
Public  Private  Protected


Copyright: MCC

Advanced Java Programming

11

Method Modifiers


Static  Abstract  Final  Native  Volatile

Copyright: MCC

Advanced Java Programming

12

Use of Modifiers
Modifier Public Private Protected Abstract Final Native Volatile
Copyright: MCC

Method Yes Yes Yes Yes Yes Yes No

Variable Yes Yes Yes No Yes No Yes


Advanced Java Programming

Class Yes Yes(nested) Yes(nested) Yes Yes No No


13

Construction
The process of bringing an object into existence is called construction  A construction - Allocates memory - Initializes attributes, if any - Enables access to attributes and methods

Copyright: MCC Advanced Java Programming

14

Constructor (cont)


Explicit constructor User defined constructor in class definition Implicit constructor Default constructor which is provided by JVM

Copyright: MCC

Advanced Java Programming

15

Destruction
The process of deleting an objects is called destruction  A destructor - Frees allocated space - Disable access to attributes and methods


Copyright: MCC

Advanced Java Programming

16

Method Overloading
In the same class and same methods name  But in different parameters and data types  Form of compile time polymorphism


Copyright: MCC

Advanced Java Programming

17

Method Overridden
In the super class as well as subclass  Allows general classes to specify methods that will common to its subclass  Form of runtime polymorphism


Copyright: MCC

Advanced Java Programming

18

Abstract class and method


There is no code in abstract class and method  Must be declared as abstract type  Can not instantiated  At least one abstract method in class


Copyright: MCC

Advanced Java Programming

19

Writing Abstract class and Method


abstract class GraphicObject { int x, y; ... void moveTo(int newX, int newY) {... } abstract void draw(); }

Copyright: MCC

Advanced Java Programming

20

Interface
A special kind of a class which consists of only the constants and the method prototypes  Practically is Java substitute of multiple inheritance  A class can implement multiple interfaces

Copyright: MCC Advanced Java Programming

21

Interface Vs Abstract Class


Feature Multiple Inheritance Default implementation Interface A class May implement several interfaces. An interface cant provide any code at all Static final constants only Abstract Class A class may extend only one abstract class An abstract class can provide complete code, default code Both instance and static constant are possible A third party class must be rewritten to extend only from the abstract class. 22

Constants

Third party convenience An interface implementation may be added to any existing third party classes
Copyright: MCC

Advanced Java Programming

Package
The group of classes and interfaces  Can be used other java standard packages  Need to specified class paths for it


Copyright: MCC

Advanced Java Programming

23

Package and Access Control


Public Same class Same package subclass Same package non-subclass Different package subclass Different package non-subclass Yes Yes Yes Yes Yes Private Yes No No No No Protected Yes Yes Yes Yes Yes No modifier Yes Yes Yes Yes yes

Copyright: MCC

Advanced Java Programming

24

Example Java Package


      

Java.lang Java.applet Java.awt Java.io Java.util Java.net Java.rmi.etc


Copyright: MCC Advanced Java Programming

25

//Constructor Example
class Constructor_test{ public Constructor_test() { System.out.println("Hello "); } public Constructor_test(String txt) { System.out.println("Hello "+txt); } public static void main(String args[]) { Constructor_test obj=new Constructor_test(); Constructor_test obj2=new Constructor_test(args[0]); } }
Copyright: MCC Advanced Java Programming

26

Method Overloading
class Method_overload{ public void Show(){ System.out.println("Hello Myanmar"); } public static int Show(int num){ int a=num+10; return a; } public static void main(String args[]){ Method_overload obj=new Method_overload(); obj.Show(); int num=Show(320); System.out.println(num); } Advanced Java Programming } Copyright: MCC

27

Inheritance Example
class Super{ public void Show(){ System.out.println("Hello"); } } class Sub extends Super{ public static void main(String args[]){ Sub obj=new Sub(); obj.Show(); } }

Copyright: MCC

Advanced Java Programming

28

Method overridden Example


class Super{ public void Show(){ System.out.println("Hello"); } } class Sub extends Super{ public void Show(int num){ System.out.println("Hello "+num); } public static void main(String args[]){ Sub obj=new Sub(); obj.Show(); obj.Show(20); } }
Copyright: MCC Advanced Java Programming

29

Example for Constructors


//Constructor and Overloaded Constructor class Me{ public Me(){ System.out.println("Hello Constructor"); } public Me(String arg){ System.out.println("Hello " +arg); } public static void main(String args[]){ Me obj=new Me(); // implicit Me obj1=new Me("OverLoaded Constructor"); } }
Copyright: MCC Advanced Java Programming

30

Method and Overloaded Method


//Method and Overloaded Method class Me{ public void Show(){ System.out.println("Hello Method"); } public void Show(String arg){ System.out.println("Hello " +arg); } public static void main(String args[]){ Me obj=new Me(); obj.Show(); obj.Show("Overloaded Method"); } }
Copyright: MCC Advanced Java Programming

31

Overridden Method
class Me{ public void Show(){ System.out.println("Hello Method"); } } class Sub extends Me{ public void Show(String arg){ System.out.println("Hello " +arg); } public static void main(String args[]){ Sub obj=new Sub(); obj.Show(); obj.Show("Overloaded Method"); } }
Copyright: MCC Advanced Java Programming

32

Generation Interface
//interface example public interface MyInterface{ public void add(int x,int y); }

Copyright: MCC

Advanced Java Programming

33

Implementing Interface
class Demo implements MyInterface{ public void add(int x,int y){ System.out.println(" "+(x+y)); } public static void main(String args[]){ Demo obj=new Demo(); obj.add(20,30); } }

Copyright: MCC

Advanced Java Programming

34

Generating Package
package MyPackage; public class Compute{ public int Add(int x,int y){ return (x+y); } public int Subtract(int x,int y){ return (x-y); } public int Multiply(int x,int y){ return (x*y); } public int Divide(int x,int y){ return (x/y); } }
Copyright: MCC Advanced Java Programming

35

Applying Package
import MyPackage.*; class PKDemo{ public static void main(String args[]){ Compute obj=new Compute(); int sum=obj.Add(10,20); int sub=obj.Subtract(20,10); System.out.println("the total is "+sum); System.out.println("the minus is "+sub); } }

Copyright: MCC

Advanced Java Programming

36