Upload
backdoor
View
30
Download
3
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
IADCS Diploma CourseObject Oriented Programming
U Nyein OoCOO/Director(IT)Myanma Computer Co., Ltd(MCC)
Copyright: MCC Advanced Java Programming 2
OOP?
Software Development TechniqueEffective use of programmer productivityWay of Creating Large Scale SystemsApproaching 100% solution
Copyright: MCC Advanced Java Programming 3
Basic Concepts
ObjectClass InheritancePolymorphismEncapsulationData Abstraction
Copyright: MCC Advanced Java Programming 4
Object
A Person, Place or ConceptsComposed of Characteristics (data)
and Behavior (Operation)Can operate directly on its data Interact sending message to each other
Copyright: MCC Advanced Java Programming 5
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 6
Inheritance
The mechanism that permits a class to share the attributes and operations defined in one or more classes
Copyright: MCC Advanced Java Programming 7
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 8
Polymorphism
The property that allows an operation to have different behavior on different objects
Copyright: MCC Advanced Java Programming 9
Encapsulation
The process of binding data and methods together is known as encapsulation
Copyright: MCC Advanced Java Programming 10
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 11
Access Specifiers
Public PrivateProtected
Copyright: MCC Advanced Java Programming 12
Method Modifiers
StaticAbstractFinalNativeVolatile
Copyright: MCC Advanced Java Programming 13
Use of Modifiers
Modifier Method Variable Class
Public Yes Yes Yes
Private Yes Yes Yes(nested)
Protected Yes Yes Yes(nested)
Abstract Yes No Yes
Final Yes Yes Yes
Native Yes No No
Volatile No Yes No
Copyright: MCC Advanced Java Programming 14
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 15
Constructor (cont)
Explicit constructor
User defined constructor in class definition
Implicit constructor
Default constructor which is provided by JVM
Copyright: MCC Advanced Java Programming 16
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 17
Method Overloading
In the same class and same methods name
But in different parameters and data typesForm of compile time polymorphism
Copyright: MCC Advanced Java Programming 18
Method Overridden
In the super class as well as subclassAllows general classes to specify
methods that will common to its subclass
Form of runtime polymorphism
Copyright: MCC Advanced Java Programming 19
Abstract class and method
There is no code in abstract class and method
Must be declared as abstract typeCan not instantiatedAt least one abstract method in class
Copyright: MCC Advanced Java Programming 20
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 21
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 22
Interface Vs Abstract Class
Feature Interface Abstract Class
Multiple Inheritance A class May implement several interfaces.
A class may extend only one abstract class
Default implementation An interface can’t provide any code at all
An abstract class can provide complete code, default code
Constants Static final constants only
Both instance and static constant are possible
Third party convenience An interface implementation may be added to any existing third party classes
A third party class must be rewritten to extend only from the abstract class.
Copyright: MCC Advanced Java Programming 23
Package
The group of classes and interfacesCan be used other java standard
packagesNeed to specified class paths for it
Copyright: MCC Advanced Java Programming 24
Public Private Protected No modifier
Same class Yes Yes Yes Yes
Same package subclass
Yes No Yes Yes
Same package non-subclass
Yes No Yes Yes
Different package subclass
Yes No Yes Yes
Different package non-subclass
Yes No Yes yes
Package and Access Control
Copyright: MCC Advanced Java Programming 25
Example Java Package
Java.lang Java.applet Java.awt Java.io Java.util Java.net Java.rmi….etc
Copyright: MCC Advanced Java Programming 26
//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 27
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);
}}
Copyright: MCC Advanced Java Programming 28
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 29
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 30
Example for Constructors
//Constructor and Overloaded Constructorclass 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(); // implicitMe obj1=new Me("OverLoaded Constructor");
}}
Copyright: MCC Advanced Java Programming 31
Method and Overloaded Method
//Method and Overloaded Methodclass 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 32
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 33
Generation Interface
//interface example public interface MyInterface{
public void add(int x,int y);}
Copyright: MCC Advanced Java Programming 34
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 35
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 36
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);
}}