WHAT IS ABSTRACT KEYWORD ?

abstract keyword can be used before a class or method ,
An abstract class can never be instantiated.Its sole purpose,is to be extended (subclassed).
if a class contains only one abstract method then the whole class must be abstract.
abstract class can be extended by an another abstract class or a concrete subclass.
if extended by abstract class then then no need to override the abstract methods, but if extended by a concrete class then it is mandatory to override the abstract method,
ex- abstract class vehicle{
abstract void wheels();}
class car extends vehicle{
void wheels(){system.out.println("a car has four wheels");}
public static void main(String[] args){
vehicle v=new car();
}
}
Notice that the methods marked abstract end in a semicolon rather than
curly braces.
we can create the reference variable of superclass by passing the sub class reference. as vehicle v=new car();
We can not use abstract and final together. Because both have opposite meaning.
We can not use abstract and static together.

3 comments:

  1. ya exactly correct but..we can only declare using abstract keyword in abstract class.. defining should be done in subclass like our class...

    raju.ponnarasu@gmail.com

    ReplyDelete
  2. ya absolutely,
    abstract class gives the method body which is already known and no need to define the body by our self,
    but it makes the methods abstract which body is not known,

    See the method is used somewhere else in the class or in another class, when you override the same method then the body is used,

    for EX suppose there is an abstract class having one abstract method display() and what ever you write inside this abstract method it is displayed in the browser, the other methods inside the class takes care about how to display in the browser,
    So you need only to give the display() method body, no need to care about how it will be displayed in the broowser,
    hope you understand

    ReplyDelete
  3. in the above example v.wheels() will give me??
    a car has four wheels is it correct?

    what if in the same example
    abstract class vehicle{
    abstract void wheels()
    public void run()
    {system.out.println("vehicle is at x speed")
    }};
    class car extends vehicle{
    void wheels(){system.out.println("a car has four wheels")
    public void run()
    {system.out.println("car is at x speed")
    };}
    public static void main(String[] args){
    vehicle v=new car();
    v.run();
    }
    }
    v.run() which class call???

    ReplyDelete