Vous êtes sur la page 1sur 8

Abstract Method in Class

Prof. Kumar Anand Singh

Prof. Kumar Anand Singh


What is Abstract Class?
• Class with one or more than one Abstract Methods

Consider the following class


hierarchy consisting of a Shape
class which is inherited by three
classes Rectangle, Circle, and
Triangle. The Shape class is
created to save on common
attributes and methods shared by
the three classes Rectangle, Circle,
and Triangle. calculateArea() is one
such method shared by all three
child classes and present in Shape
class.
Prof. Kumar Anand Singh
An object of the An object of the class But what would an
class rectangle will triangle will give a object of Class Shape
give a rectangle, a triangle, again a common look like in a
shape we so everyday shape. practical world ??
commonly observed
in everyday life.

Syntax:

If you observe the Shape class serves in our goal of achieving abstract class Shape{
inheritance and polymorphism. But it was not built to be // code
instantiated. Such classes can be labeled Abstract. An abstract }
class can not be instantiated.
Prof. Kumar Anand Singh
What are Abstract Methods?
• An Abstract Method is a method that has just the method definition but
does not contain implementation.
• As we all know, the formula for calculating area for rectangle, circle, &
triangle is different. The calculateArea() method will have to be
overridden by the inheriting classes. It makes no sense defining it in the
Shape class, but we need to make sure that all the inheriting classes do
have the method.
• Such methods can be labeled abstract.
Syntax:
abstract public void calculateArea();

Prof. Kumar Anand Singh


Abstract Class in Java: Important Points
• An abstract class may also have concrete (complete) methods.
• For design purpose, a class can be declared abstract even if it does not
contain any abstract methods
• Reference of an abstract class can point to objects of its sub-classes
thereby achieving run-time polymorphism Ex: Shape obj = new
Rectangle();
• A class must be compulsorily labeled abstract, if it has one or more
abstract methods.

Prof. Kumar Anand Singh


Examples: To learn abstract & final keywords
1. abstract class Shape{ Save , Compile & Run the code.
2. final int b = 20;
Error =? The abstract method is not implemented int
3. public void display(){ the class Rectangle. To fix the issue uncommnet line
4. System.out.println("This is display method"); #15.
5. }
6. abstract public void calculateArea(); Uncomment line # 14 . Save & Compile the code.
7. }
Error = ? variable b is final
8. public class Rectangle extends Shape{
9. public static void main(String args[]){
10. Rectangle obj = new Rectangle();
11. obj.display();
12. //obj.b=200;
13. }
14. //public void calculateArea(){}
15. }

Prof. Kumar Anand Singh


Rules of Abstract Method
1. Abstract methods don’t have body, they just have method signature as
shown above.
2. If a class has an abstract method it should be declared abstract, the
vice versa is not true, which means an abstract class doesn’t need to
have an abstract method compulsory.
3. If a regular class extends an abstract class, then the class must have to
implement all the abstract methods of abstract parent class or it has to
be declared abstract as well.

Prof. Kumar Anand Singh


More Examples //Regular class extends abstract class
class Demo extends Sum{
//abstract class
abstract class Sum{ /* If I don't provide the implementation of these two
methods, the
/* These two are abstract methods, the child class
* program will throw compilation error.
* must implement these methods */
*/ public int sumOfTwo(int num1, int num2){
public abstract int sumOfTwo(int n1, int n2); return num1+num2;
}
public abstract int sumOfThree(int n1, int n2, int
n3); public int sumOfThree(int num1, int num2, int num3){
return num1+num2+num3;
}
//Regular method public static void main(String args[]){
public void disp(){ Sum obj = new Demo();
System.out.println("Method of class Sum"); System.out.println(obj.sumOfTwo(3, 7));
} System.out.println(obj.sumOfThree(4, 3, 19));
obj.disp();
} }
Output: }
10
26
Method of class Sum Prof. Kumar Anand Singh

Vous aimerez peut-être aussi