Answer Posted / ankur pandya
When we want to design such a method which is different for
every of its child class, means all the subclass has that
method with different behavior, then we declare the method
as ABSTRACT METHOD.
for eg.
------------------------------------------------------------
// file name "AbstractMethodDemo.java"
abstract class MyAbstractClass {
abstract void myAbstractClassMethod1(); // defination
done in DerivedClass class
void myAbstractClassMethod2(){
System.out.println("myAbstractClassMethod2()
method called from Parent Class..");
}
}
class DerivedClass1 extends MyAbstractClass {
void myAbstractClassMethod1(){
System.out.println(" myAbstractClassMethod1()
method Called form Derived Class-1..");
}
}
class DerivedClass2 extends MyAbstractClass {
void myAbstractClassMethod1(){
System.out.println(" myAbstractClassMethod1()
method Called form Derived Class-2..");
}
}
public class AbstractMethodDemo {
public static void main(String[] args) {
DerivedClass1 obj1 = new DerivedClass1();
obj1.myAbstractClassMethod1();
obj1.myAbstractClassMethod2();
DerivedClass2 obj2 = new DerivedClass2();
obj2.myAbstractClassMethod1();
obj2.myAbstractClassMethod2();
}
}
---------------------------------------------------------
Here myAbstractClassMethod1() is abstract because I want to
change its behaviour as Base classes, But
myAbstractClassMethod2() is not declared Abstract because I
want to keep its behaviour same for every base class.
Ans obvious, Abstract Method must be declared in Abstract Class.
I hope the defination is clear now..
Is This Answer Correct ? | 9 Yes | 0 No |
Post New Answer View All Answers
Which sorting is used in arrays sort in java?
Difference between method overloading and method overriding in java ?
How do you start a thread?
Why main method is static in java?
What is merge sort in java?
What is difference between static and abstract class?
How do you override a method in java?
What is the concatenation operator in java?
Difference between string, stringbuffer and stringbuilder?
What is pass by value?
Why does it take so much time to access an applet having swing components the first time?
Can we have a try block without catch block?
Lowest Common ancestor in a Binary Search Tree and Binary Tree.
Why to use nested classes in java? (Or) what is the purpose of nested class in java?
What language is pass by reference?