Results 1 to 5 of 5

Thread: Clarification regarding Java Interface and Inheritance

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Geek_Guest
    Guest

    Clarification regarding Java Interface and Inheritance

    Java Specification says that we cannot create an object for an Interface but in the init (ServletConfig config) Syntax we are passing an object of ServleConfig which is an interface. Similarly in the service method we pass ServletRequest, ServletResponse objects.

    Please clarify Java says that Multiple inheritance is not possible but every class extends Object class and we can extend a class explictly. It means that a class is extending 2 classes please clarify

    Question asked by visitor suman


  2. #2
    Junior Member
    Join Date
    Mar 2007
    Answers
    10

    Re: Clarification regarding Java Interface and Inheritance

    Hi Suman,

    We cant create an object to Interface but we can assign an object reference.

    For example:
    A -> Interface
    B -> Class implements A

    We can set a reference to Interface object likethis:
    A newObject = new B();

    In init method we are passing ServeltConfig object reference not creating new object. The object will be created by webserver which contains Implementation classes of ServeltConfig Interface. Same case for service() also.

    Java doesn't support multiple inheritance for Classes. Every class extends java.lang.Object class if it doesn't extend any other class. Try the following example you can know the difference.

    public class A { }

    public class B extends A { }

    compile those two classes.
    run the following commands:
    javap A
    javap B

    check the output and find the difference

    Regards,
    Eswar


  3. #3
    Junior Member
    Join Date
    Jun 2007
    Answers
    2

    Re: Clarification regarding Java Interface and Inheritance

    1. Although we can't create object of interfaces but can create reference of it which we are sending.


    2. what u r considering is not multiple inheritence but multi level inheritence
    in multiple inher.. two classes/ entites are simultaneously extending by third one.


  4. #4
    Junior Member
    Join Date
    Jun 2007
    Answers
    2

    Re: Clarification regarding Java Interface and Inheritance

    1. Although we can't create object of interfaces but can create reference of it which we are sending.


    2. what u r considering is not multiple inheritence but multi level inheritence
    in multiple inher.. two classes/ entites are simultaneously extending by third one.


  5. #5
    Junior Member
    Join Date
    Apr 2007
    Answers
    15

    Re: Clarification regarding Java Interface and Inheritance

    Hi,

    for your first doubt, since u r passing the arguments (ServletRequest request),
    the object is not created.it'll be created only when ServletRequest request=new ServletRequest();

    for your second doubt,
    if a class extends a class, then it won't extend the Object class directly,
    it will extend through that super class only


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact