• Abstract class and Object class in Java

    ABSTRACT CLASS and OBJECT CLASS

    There a situation in which we will want to define a super class that declares the structure of a given abstraction without providing a complete implementation of every method that is we will want to a create a super class that only defines a generalize form that will be shared by all of its sub-classes living it to each sub class to fill the detail. It is very common for a method to have no meaningful definition in the context of its super class .Hence we may have methods that must be overridden by the sub-classes to have any meaning. To ensure that sub-class override all necessary methods abstract method is used .

    ABSTRACT CLASS  

    Any class contains are or more abstract method must also be declared abstract . To declare a class abstract we simply use the abstract keyword in front of the class keyword at the beginning of the class declaration.

    CHARACTERISTICS :

    1.An abstract class can not be directly instantiated with the new operator since it is not fully defined .
    2.We can not declare any constructor of an abstract class .
    3. An abstract class can not contain any static method .
    4. Any subclass of an abstract class must either implemented all abstract method or be declared abstract itself.

    CODE:
    abstract class A
    {
    Int a ;
    Abstract void show()
    }
    class B extends A
    {
    Void show()
    {
    System.out.println(a);
    }
    public static void main(String []args )
    {
    B b = new B();
    b.show();
    }

    }

    Since show method is abstract it is the responsibility of class B that inherited it ton provide its implementation .

    OBJECT-CLASS 

    There is a special class called object-class which is defined by java, all other classes are subclass of object . A reference variable of type object can refer to an object of any other class , since it is superclass of all other classes .Also since arrays are implemented as classes , a variable of type object can also refer to any array. The methods defines by object are available to every other object. Some of these methods are discussed below ---

    METHOD
    PURPOSE
    Object clone()
    create a new object that is same as the object being cloned.
    void finalise()
    called before an unused object is recycled
    boolean equals(Object o)
    determines whether one object is equal to another.
    String toString()
    returns a string that describes the object.
               










    All the methods defined in object except getclass(), notify() , notifyAll() and wait() are not declared as final and hence can be overridden.

  • You might also like

    No comments:

    Post a Comment

search topics

NEWSLETTER

Get All The Latest Updates Delivered Straight Into Your Inbox For Free!