When you declare a method as abstract method ?
Answers were Sorted based on User's Feedback
Answer / 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 |
Answer / shadow
When i want child class to implement the behavior of the
method.
| Is This Answer Correct ? | 5 Yes | 2 No |
hoe can u call a constructor of a private classs to other inherited claa??
What is core java used for?
What is the preferred size of a component?
What is a class variable?
What is protected access modifier?
Why ArrayList class is not a synchronized class and why it is not a thread safe class? explain
Explain the overview of UDP messaging.
What is a native method in java programming?
What is exception in java?
What is the differnence between String Buffer and String builder despite having knowledge that String builder is faster than String Buffer and last one is threadsafe.please tell another important difference.
What is use of a abstract variable?
Can we have any code between try and catch blocks?