Abstract classes and Methods in java

1,067 views

Published on

Index
Definition
Usefulness
Why they cannot be instantiated?
How to use abstract classes?
Purpose of abstract class
What is Concrete class?
Examples
Characteristics explained

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,067
On SlideShare
0
From Embeds
0
Number of Embeds
659
Actions
Shares
0
Downloads
46
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Abstract classes and Methods in java

  1. 1. Abstract classes and Methods in Java A Presentation in Depth
  2. 2. Index Definition Usefulness Why they cannot be instantiated? How to use abstract classes? Purpose of abstract class What is Concrete class? Examples Characteristics explained
  3. 3. Definition Abstract classes—for which you never intend to create objects.
  4. 4. Useful They’re used only as superclasses in inheritance hierarchies, we refer to them as abstract superclasses.
  5. 5. Why they cannot be instantiated? 1. These classes cannot be used to instantiate objects, because abstract classes are incomplete. 2. Abstract superclasses are too general to create real objects—they specify only what is common among subclasses.
  6. 6. So how can we use them? Or instantiate them? Subclasses must declare the “missing pieces” to become “concrete” classes, from which you can instantiate objects.
  7. 7. What is the purpose of abstract class? An abstract class’s purpose is to provide an appropriate superclass from which other classes can inherit and thus share a common design.
  8. 8. What is concrete class? Classes that can be used to instantiate objects are called concrete classes. Such classes provide implementations of every method they declare (some of the implementations can be inherited). Concrete classes provide the specifics that make it reasonable to instantiate objects.
  9. 9. Example 1
  10. 10. Example 2
  11. 11. Characteristics summary
  12. 12. Characteristics summary 1. Abstract class can be empty.
  13. 13. Characteristics summary 1. Abstract class can be empty. 2. Abstract class can be made without abstract methods.
  14. 14. Characteristics summary 1. Abstract class can be empty. 2. Abstract class can be made without abstract methods. 3. A non-abstract class cannot contain abstract method.
  15. 15. Abstract class can be empty
  16. 16. Abstract class can be empty abstract class Vehicle{}
  17. 17. Abstract class can be empty abstract class Vehicle{} Contains no members
  18. 18. Abstract class can be empty abstract class Vehicle{} Perfectly valid Contains no members
  19. 19. Abstract class can be made without abstract methods
  20. 20. Abstract class can be made without abstract methods abstract class Vehicle { void brake() { System.out.println("non abstract method brake"); } }
  21. 21. Abstract class can be made without abstract methods abstract class Vehicle { void brake() { System.out.println("non abstract method brake"); } } Non abstract method
  22. 22. Abstract class can be made without abstract methods abstract class Vehicle { void brake() { System.out.println("non abstract method brake"); } } Non abstract method Perfectly valid
  23. 23. class Vehicle { abstract void brake(); }
  24. 24. class Vehicle { abstract void brake(); } Invalid/compilation error
  25. 25. A non-abstract class cannot contain abstract method class Vehicle { abstract void brake(); } Invalid/compilation error
  26. 26. A non-abstract class cannot contain abstract method class Vehicle { abstract void brake(); } Invalid/compilation error In other words, if a class contains abstract method then class must also be abstract.
  27. 27. A non-abstract class cannot contain abstract method class Vehicle { abstract void brake(); } Invalid/compilation error Now valid abstract class Vehicle { abstract void brake(); } In other words, if a class contains abstract method then class must also be abstract.
  28. 28. Example 3
  29. 29. Example 4
  30. 30. Characteristics summary
  31. 31. Characteristics summary 4. Class can be final or abstract, not both.
  32. 32. Characteristics summary 4. Class can be final or abstract, not both. 5. Method be can be final or abstract not both.
  33. 33. Class can be abstract or final, not both
  34. 34. Class can be abstract or final, not both Error
  35. 35. Method can be abstract or final, not both
  36. 36. Method can be abstract or final, not both Error
  37. 37. Real Examples of Abstract Classes in Java API 1. java.awt.Component Component Button Checkbox Label TextComponent
  38. 38. Real Examples of Abstract Classes in Java API 2. java.lang.Number Number Byte Long Integer Float Double
  39. 39. Real Examples of Abstract Classes in Java API 3. javax.swing.AbstractButton AbstractButton JButton JToggleButton JMenuItem
  40. 40. Characteristics summary
  41. 41. Characteristics summary 6. Non-abstract class cannot contain abstract methods even there are non abstract methods too.
  42. 42. Characteristics summary 6. Non-abstract class cannot contain abstract methods even there are non abstract methods too. 7. An abstract class can contain both abstract and non abstract methods.
  43. 43. Characteristics summary 6. Non-abstract class cannot contain abstract methods even there are non abstract methods too. 7. An abstract class can contain both abstract and non abstract methods. 8. Abstract class can be inherited like normal class.
  44. 44. Characteristics summary 6. Non-abstract class cannot contain abstract methods even there are non abstract methods too. 7. An abstract class can contain both abstract and non abstract methods. 8. Abstract class can be inherited like normal class. 9. If abstract class contains no abstract methods then subclass of it, can be empty.
  45. 45. Non-abstract class cannot contain abstract methods even there are non-abstract methods too
  46. 46. Non-abstract class cannot contain abstract methods even there are non-abstract methods too Non abstract method
  47. 47. Non-abstract class cannot contain abstract methods even there are non-abstract methods too Abstract method Non abstract method
  48. 48. Non-abstract class cannot contain abstract methods even there are non-abstract methods too Abstract method Non abstract method Either make the class abstract or make method non abstract to correct this error
  49. 49. An abstract class can contain both abstract and non abstract methods
  50. 50. An abstract class can contain both abstract and non abstract methods Non abstract method
  51. 51. An abstract class can contain both abstract and non abstract methods Abstract method Non abstract method
  52. 52. Abstract class can be inherited like normal class If abstract class is empty then subclass can also be empty.
  53. 53. Abstract class can be inherited like normal class If abstract class is empty then subclass can also be empty.
  54. 54. Abstract class can be inherited like normal class No error If abstract class is empty then subclass can also be empty.
  55. 55. If abstract class contains no abstract methods then subclass of it, can be empty
  56. 56. If abstract class contains no abstract methods then subclass of it, can be empty
  57. 57. If abstract class contains no abstract methods then subclass of it, can be empty Perfectly valid
  58. 58. Example 5
  59. 59. Example 6
  60. 60. Example 7
  61. 61. Characteristics summary
  62. 62. Characteristics summary 10. If abstract class contains one or more abstract methods then subclass of it, can not be empty.
  63. 63. Characteristics summary 10. If abstract class contains one or more abstract methods then subclass of it, can not be empty. 11. If abstract class contains one or more abstract methods then subclass of it, can be empty, only if subclass is also abstract.
  64. 64. Characteristics summary 10. If abstract class contains one or more abstract methods then subclass of it, can not be empty. 11. If abstract class contains one or more abstract methods then subclass of it, can be empty, only if subclass is also abstract. 12. If a abstract class contains abstract methods then subclass must have to implements(write code) for abstract methods, if subclass does not want to be abstract.
  65. 65. If abstract class contains one or more abstract methods then subclass of it, can not be empty
  66. 66. If abstract class contains one or more abstract methods then subclass of it, can not be empty
  67. 67. If abstract class contains one or more abstract methods then subclass of it, can not be empty Error
  68. 68. If abstract class contains one or more abstract methods then subclass of it, can not be empty Error
  69. 69. If abstract class contains one or more abstract methods then subclass of it, can not be empty Error There are two ways to correct this error either implement abstract methods in subclass or make subclass abstract.
  70. 70. If abstract class contains one or more abstract methods then subclass of it, can not be empty Error There are two ways to correct this error either implement abstract methods in subclass or make subclass abstract. Next slides will show how to remove this error
  71. 71. If abstract class contains one or more abstract methods then subclass of it, can be empty, only if subclass is also abstract
  72. 72. If abstract class contains one or more abstract methods then subclass of it, can be empty, only if subclass is also abstract
  73. 73. If abstract class contains one or more abstract methods then subclass of it, can be empty, only if subclass is also abstract Perfectly valid
  74. 74. If a abstract class contains abstract methods then subclass must have to implements(write code) for abstract methods, if subclass does not want to be abstract
  75. 75. If a abstract class contains abstract methods then subclass must have to implements(write code) for abstract methods, if subclass does not want to be abstract Perfectly valid
  76. 76. Characteristics summary
  77. 77. Characteristics summary 13.Abstract class can implement super class abstract methods.
  78. 78. Characteristics summary 13.Abstract class can implement super class abstract methods. 14.Abstract classes can contain final methods, constructors, static methods.
  79. 79. Characteristics summary 13.Abstract class can implement super class abstract methods. 14.Abstract classes can contain final methods, constructors, static methods. 15.An abstract class cannot be instantiated, but we can make reference of this class.
  80. 80. If abstract class contains one or more abstract methods then subclass of it, can be abstract and still can implements super class methods.
  81. 81. If abstract class contains one or more abstract methods then subclass of it, can be abstract and still can implements super class methods.
  82. 82. If abstract class contains one or more abstract methods then subclass of it, can be abstract and still can implements super class methods. Abstract super class
  83. 83. If abstract class contains one or more abstract methods then subclass of it, can be abstract and still can implements super class methods. Abstract super class Abstract sub class
  84. 84. If abstract class contains one or more abstract methods then subclass of it, can be abstract and still can implements super class methods. Abstract super class Abstract sub class In other words, abstract class can implement super class abstract methods
  85. 85. If abstract class contains one or more abstract methods then subclass of it, can be abstract and still can implements super class methods. Abstract super class Abstract sub class Perfectly valid In other words, abstract class can implement super class abstract methods
  86. 86. Abstract classes can contain constructors
  87. 87. Abstract classes can contain static methods
  88. 88. Abstract classes can contain static methods
  89. 89. Abstract classes can contain static methods
  90. 90. Abstract classes can contain static methods Output
  91. 91. Abstract classes can contain final methods
  92. 92. Abstract classes can contain final methods Perfectly valid
  93. 93. An abstract class cannot be instantiated
  94. 94. An abstract class cannot be instantiated We can make reference of abstract class
  95. 95. An abstract class cannot be instantiated We can make reference of abstract class Error
  96. 96. Example 8
  97. 97. Example 9
  98. 98. Characteristics summary 8. Abstract methods cannot be private. They can have public, protected or default access specifier. 9. Abstract class can extend non-abstract class.
  99. 99. Abstract methods cannot be private
  100. 100. Abstract methods cannot be private Error
  101. 101. Abstract methods can use public access specifier
  102. 102. Abstract methods can use protected access specifier
  103. 103. Abstract methods can use default access specifier
  104. 104. Abstract class can inherit non-abstract class
  105. 105. Abstract class can inherit non-abstract class Perfectly valid
  106. 106. Complete Characteristics summary 1. 2. 3. 4. 5. 6. 7. Abstract class can be empty.(slide 2) Abstract class can be made without abstract methods. (slide 3) A non-abstract class cannot contain abstract method.(slide 4) Non-abstract class cannot contain abstract methods even there are non abstract methods too.(slide 5) An abstract class can contain both abstract and non abstract methods.(slide 6) Abstract class can be inherited like normal class(slide 7) If abstract class contains no abstract methods then subclass of it, can be empty(slide 8)
  107. 107. Complete Characteristics summary 8. 9. 10. 11. 12. 13. 14. 15. If abstract class contains one or more abstract methods then subclass of it, can not be empty(slide 9) If abstract class contains one or more abstract methods then subclass of it, can be empty, only if subclass is also abstract(slide 10) If a abstract class contains abstract methods then subclass must have to implements(write code) for abstract methods, if subclass does not want to be abstract(slide 11) Abstract class can implement super class abstract methods. (slide 12) Abstract classes can contain final methods, constructors, static methods.(slide 13,14,15) An abstract class cannot be instantiated, but we can make reference of this class.(slide 16) Abstract methods cannot be private. They can have public, protected or default access specifier.(slide 17,18,19,20) Abstract class can extend non-abstract class.(slide 21)
  108. 108. Complete Characteristics summary 17.Class can be final or abstract, not both.(slide 22) 18.Method be can be final or abstract not both.(slide 23)

×