The document discusses key concepts of object-oriented programming including objects, classes, inheritance, polymorphism, encapsulation, and abstraction. It provides examples of constructors, method overloading and overriding, interfaces, and packages in Java.
13. Use of Modifiers No Yes No Volatile No No Yes Native Yes Yes Yes Final Yes No Yes Abstract Yes(nested) Yes Yes Protected Yes(nested) Yes Yes Private Yes Yes Yes Public Class Variable Method Modifier
14.
15.
16.
17.
18.
19.
20.
21.
22. Interface Vs Abstract Class A third party class must be rewritten to extend only from the abstract class. An interface implementation may be added to any existing third party classes Third party convenience Both instance and static constant are possible Static final constants only Constants An abstract class can provide complete code, default code An interface can’t provide any code at all Default implementation A class may extend only one abstract class A class May implement several interfaces. Multiple Inheritance Abstract Class Interface Feature
23.
24. Package and Access Control yes Yes No Yes Different package non-subclass Yes Yes No Yes Different package subclass Yes Yes No Yes Same package non-subclass Yes Yes No Yes Same package subclass Yes Yes Yes Yes Same class No modifier Protected Private Public
25.
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]); } }
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); } }
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(); } }
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); } }
30. 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"); } }
31. 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"); } }
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"); } }
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); } }
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); } }
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); } }