When you declare a method as abstract method ?

Answers were Sorted based on User's Feedback



When you declare a method as abstract method ?..

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

When you declare a method as abstract method ?..

Answer / shadow

When i want child class to implement the behavior of the
method.

Is This Answer Correct ?    5 Yes 2 No

Post New Answer

More Core Java Interview Questions

What is keyword in oop?

0 Answers  


What are constructors in java?

0 Answers  


Is there any difference between synchronized methods and synchronized statements?

0 Answers  


What JNDI(Java Naming and Directory Interface) provides?

4 Answers  


What are sets in java?

0 Answers  






Why do we need data structure in java?

0 Answers  


Explain about static nested classes in java?

0 Answers  


class a extends b { } class b extends a { } why java doesn't support cyclic pls explain me with example

3 Answers   Wipro,


if the memory capacity is 700 presently occupied by process is 690. then another process request space(40) how this situation handled in java.

4 Answers   Wipro,


What are bind parameters?

0 Answers  


What will happen if a thrown exception is not handled?

0 Answers   ABB, Akamai Technologies, Infogain,


Can we have any other return type than void for main method?

0 Answers  


Categories