STATIC VARIABLE/ METHODS, INHERITANCE, INTERFACE AND COMMAND LINE ARGUMENTS Example 1: Static Member Variable
1. /*
2. This Java Example shows how to declare and use static member variable inside a java class.
3. */ 4.
5. public class StaticMemberExample 6. {
7. public static void main(String[] args) 8. {
9. ObjectCounter object1 = new ObjectCounter(); 10. System.out.println(object1.getNumberOfObjects()); 11.
12. ObjectCounter object2 = new ObjectCounter(); 13. System.out.println(object2.getNumberOfObjects()); 14. } 15. } 16. 17. class ObjectCounter 18. { 19. /*
20. * Static members are class level variables and shared by all 21. * the objects of the class.
22. * To define static member, use static keyword 23. * e.g. static int i=0;
24. *
25. * Please note that static member variables can be accessed 26. * inside non static methods because they are class level 27. * variables.
28. */
29.
30. static int counter=0; 31.
32. public ObjectCounter()
33. {
34.
35. /*increase the object counter. Since only one varible 36. *is shared between all objects of this class, it 37. *always return number of objects till now.
38. */
39. counter++;
40. }
41.
42. //returns number of objects created till now 43. public int getNumberOfObjects()
44. {
45. return counter;
46. }
47. }
Example 2: Static Method
1. /*
2. This Java Example shows how to declare and use static methods inside 3. a java class.
4. */ 5.
6. public class StaticMethodExample 7. {
8.
9. public static void main(String[] args) 10. {
11.
12. int result = MathUtility.add(1, 2);
13. System.out.println("(1+2) is : " + result); 14. } 15. 16. } 17. 18. class MathUtility 19. { 20. 21. /*
22. * To declare static method use static keyword.
23. * Static methods are class level methods and can not access 24. * any instance
25. * member directly. However, it can access member's of a 26. * particular object
27. * using its reference.
28. *
29. * Static methods are generally written as a utility method or 30. * it performs task for all objects of the class.
31. *
32. */
33.
34. public static int add(int first, int second)
35. {
36. return first + second;
37. }
38. }
Example 3: Inheritance
1. //Inheritance, In Java You Can Only Inherit from One Class, 2. //Multiple Inheritance is not allowed
3. 4. class Parent 5. { 6. //data members 7. private int a; 8. public int b; 9. protected int c; 10. 11. //default constructor 12. public Parent() 13. { 14. a = b = c = 0;
15. System.out.println("I am class A's default constructor"); 16. }
17.
18. //parameterized constructor
19. public Parent(int a, int b, int c) 20. { 21. this.a = a; 22. this.b = b; 23. this.c = c; 24. } 25.
26. //get and set methods for a 27. public int geta()
28. {
29. return a; 30. }
31.
32. public void seta(int a) 33. {
34. this.a = a; 35. }
36.
37. //get and set methods for b 38. public int getb()
39. {
40. return b; 41. }
42.
43. public void setb(int b) 44. {
45. this.b = b; 46. }
47.
48. //get and set methods for c 49. public int getc()
50. {
51. return c; 52. }
53. public void setc(int c) 54. { 55. this.c = c; 56. } 57. } 58.
59. //child class inherit the properties of Parent 60. public class Child extends Parent
61. {
62. //default constructor 63. public Child()
64. {
65. System.out.println("I am class B's default constructor");
66. }
67.
68. //parameterized constructor
69. public Child(int a, int b, int c)
70. {
71. //calling base class (A's) parameterized constructor 72. super(a,b,c);
73. }
74.
75. public static void main(String args[])
76. {
77. Child obj1 = new Child(); 78. Child obj2 = new Child(1,2,3); 79.
80. //Assigning Child's Reference To Parent 81. Parent objP = new Child();
82.
83. System.out.println("\nThe value of a can-not be accessed directly in child bcz its private in parent");
84. System.out.println("\nThe value of b = " + obj2.b); 85. System.out.println("\nThe value of c = " + obj2.c);
86. }
87. }
Example 4: Constructor Call Sequence in Inheritance
1. //Constructor call sequence in inheritance 2. 3. class GrandFather 4. { 5. //Default Constructor 6. public GrandFather() 7. {
8. System.out.println("Grand Father's Constructor");
9. }
10. } 11.
12. class Father extends GrandFather 13. { 14. //Default Constructor 15. public Father() 16. { 17. System.out.println("Father's Constructor"); 18. } 19. } 20.
21. class Child1 extends Father 22. { 23. //Default Constructor 24. public Child1() 25. { 26. System.out.println("Child1's Constructor"); 27. } 28. } 29.
30. class Child2 extends Father 31. { 32. //Default Constructor 33. public Child2() 34. { 35. System.out.println("Child2's Constructor"); 36. } 37. } 38. 39. class CallingCons 40. { 41. //Main function
42. public static void main(String []args) 43. {
44. Child1 objC1 = new Child1(); 45. System.out.println();
46. Child2 objC2 = new Child2(); 47. }
48. }
Example 5: Usage of super
1. /*usage of super, method overriding and member overriding*/ 2.
3. class Parent 4. {
5. int num; 6.
7. //Function to tell about the class 8. void TellAbout()
9. {
10. System.out.println("Hello, I am Parent called by super"); 11. }
12. } 13.
14. class Child extends Parent 15. {
16. //hidding num1 of Parent 17. int num;
18.
19. //paremetrized constructor of child 20. public Child(int num1, int num2)
21. {
22. super.num = num1; 23. num = num2;
24. }
25.
26. //function to display the vlaues 27. void Display()
28. {
29. System.out.println("num in Parent Class = " + super.num); 30. System.out.println("num in Child Class = " + num);
31. }
32.
33. //function to tell about the class, (overridden method) 34. void TellAbout() 35. { 36. super.TellAbout(); 37. System.out.println("Hello, I am child"); 38. } 39. } 40. 41. class UsingSuper
Example 6: Abstract Class
1. /*abstract classes and abstract method.
2. *You cant intansiate the object of abstract class. 3. *To access the functionality of the abstract class 4. *you have to inherit it into sub-class
5. *you can provide only declaration of methods with abstract key-word 6. *you can provide implementation of methods in it
7. *you have to provide the defination of abstract methods in sub-class 8. *otherwise the sub-class becomes abstract as well*/
9. abstract class DrawingShapes 10. {
11. //data member 12. String ShapeType;
13. //abstract mehtod whose implementation required in sub-class 14. abstract void draw();
15. //con-crete methods are still allowed in abstract class 16. void BackGroundColor(String color)
17. {
18. System.out.println(ShapeType + " Drawn With Back-Ground-Color " + color); 19. }
20. }
21. //Circle class, extending the DrawingShapes 22. class Circle extends DrawingShapes
23. {
24. //implementation of abstract method 25. void draw()
26. {
27. ShapeType = "Circle"; 28. }
29. }
30. //Rectangle class, extending the Drawing Shpaes 31. class Rectangle extends DrawingShapes
32. {
33. //implementation of abstract method 34. void draw()
35. {
36. ShapeType = "Rectangle"; 37. }
39. class UsingAbstract 40. {
41. public static void main(String []args) 42. {
43. //object of type Circle 44. Circle c = new Circle(); 45.
46. //object of type Rectangle 47. Rectangle r = new Rectangle(); 48.
49. //obtain a reference of type DrawingShapes (abstract class) 50. DrawingShapes ds;
51.
52. //calling draw method of Circle class 53. c.draw();
54.
55. //calling BackGroundColor method of Circle class 56. c.BackGroundColor("Yellow");
57.
58. //ds of abstract class, refers to Rectangle class's object 59. ds = r;
60.
61. //calling draw mehtod of Rectangle class 62. ds.draw();
63.
64. //calling BackGroundColor method of Rectangle class 65. ds.BackGroundColor("Blue");
66. } 67. }
Example 7: Creating and Using Interface
1. //declaring a simple interface that contains five abstract mehtods 2. interface ISimpleMath
3. {
4. float Add(float num1, float num2); 5. float Sub(float num1, float num2); 6. float Mul(float num1, float num2); 7. float Div(float num1, float num2); 8. float Rem(float num1, float num2); 9. }
10.
11. //implementing the ISimpleMath interface (declared above) in //Calculator class
12. class Calculator implements ISimpleMath 13. {
14. //implementation of the interface methods 15. //implementing Add(Addition) function 16. public float Add(float num1, float num2) 17. {
18. return num1 + num2; 19. }
20.
21. //implementing Sub(Subtraction) function 22. public float Sub(float num1, float num2) 23. {
24. return num1 - num2; 25. }
26.
27. //implementing Mul(multiply) function 28. public float Mul(float num1, float num2) 29. {
30. return num1 * num2; 31. }
32.
33. //implementing Div(Division) function 34. public float Div(float num1, float num2) 35. {
36. return num1 / num2; 37. }
38.
39. //implementing Rem(Remainder) function 40. public float Rem(float num1, float num2) 41. {
42. return num1 % num2; 43. }
44.
45. //get and set property methods for num1 46. public float GetNum1()
47. {
48. return num1; 49. }
50. 51.
54. public void SetNum1(float num1) 55. {
56. this.num1 = num1; 57. }
58.
59. //get and set property methods for num2 60. public float GetNum2()
61. {
62. return num2; 63. }
64.
65. public void SetNum2(float num2) 66. {
67. this.num2 = num2; 68. }
69.
70. //class function to take input, by using Keyboard class 71. void TakeInput()
72. {
73. System.out.print(" Enter First Number = "); 74. num1 = Keyboard.readFloat();
75.
76. System.out.print(" Enter Second Number = "); 77. num2 = Keyboard.readFloat();
78. } 79.
80. //class function to display results
81. void Display(float a, float s, float m, float d, float r) 82. {
83. System.out.println(" Sum = " + a); 84. System.out.println(" Sub = " + s); 85. System.out.println(" Mul = " + m); 86. System.out.println(" Div = " + d); 87. System.out.println(" Rem = " + r); 88. } 89. 90. //Main Function
91. public static void main(String args[]) 92. {
93. //Creating an object of Calculator class 94. Calculator objC = new Calculator(); 95.
109. //class data members 110. private float num1; 111. private float num2; 112. }
Output
Example 8: Displaying Command Line Arguments Passed to the Program
1. class CommandLineArgs 2. {
3. public static void main(String []args) 4. {
5. //getting the length of passed arguments 6. int len = args.length;
7. if(len==0)
8. {
9. System.out.println("No Argument(s) Have Been Passed"); 10. }
11. else 12. {
13. //displaying the passed arguments 14. for(int i=0; i<len; i++)
15. System.out.println("The Argument At Location args[ "+ i + " ] is " + args[i]); 16. }
17. } 18. }