Professional Documents
Culture Documents
OOP?
Software Development Technique Effective use of programmer productivity Way of Creating Large Scale Systems Approaching 100% solution
Copyright: MCC
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
Class
Structure that contains common data and function as entity for objects Each object is an instance of a class
Copyright: MCC
Inheritance
The mechanism that permits a class to share the attributes and operations defined in one or more classes
Copyright: MCC
Inheritance(cont)
Subclass
Super class
Multiple Inheritance
Copyright: MCC
Polymorphism
The property that allows an operation to have different behavior on different objects
Copyright: MCC
Encapsulation
Copyright: MCC
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
11
Method Modifiers
Copyright: MCC
12
Use of Modifiers
Modifier Public Private Protected Abstract Final Native Volatile
Copyright: MCC
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
15
Destruction
The process of deleting an objects is called destruction A destructor - Frees allocated space - Disable access to attributes and methods
Copyright: MCC
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
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
18
Copyright: MCC
19
Copyright: MCC
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
Constants
Third party convenience An interface implementation may be added to any existing third party classes
Copyright: MCC
Package
The group of classes and interfaces Can be used other java standard packages Need to specified class paths for it
Copyright: MCC
23
Copyright: MCC
24
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
28
29
30
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
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
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
36