This discussion is archived
9 Replies Latest reply: Jan 17, 2012 12:13 AM by 911262 RSS

Java OO design

911262 Newbie
Currently Being Moderated
Hello Everyone,

We are knowing that in java constructor are never inherited then what is the need of access modifiers for constructor..???
  • 1. Re: Java OO design
    gimbal2 Guru
    Currently Being Moderated
    908259 wrote:
    Hello Everyone,

    We are knowing that in java constructor are never inherited then what is the need of access modifiers for constructor..???
    Think about it differently - treat access modifiers as tools in your toolbox, not language rules. Access modifiers give you the ability to put restrictions in how and more importantly -when- a certain method (or constructor) can be called. When you give a class a private constructor, will you be able to create an instance of it in another class? No. But think about this:
    public class Animal {
    
      private String name;
    
      private Animal(String name){
        this.name = name;
      }
    
      public static Animal createMonkey(){
        return new Animal("monkey");
      }
    
      public static Animal createBear(){
        return new Animal("bear");
      }
    }
    Its a silly example; A class can call its own private members and that extends to static methods part of that class; this means you can construct Animal objects even though Animal has a private constructor. Like this you can only create Animal objects through these static methods and you have 100% control over how the objects are initialized.

    You see? Tools in your toolbox. Use them wisely.
  • 2. Re: Java OO design
    911262 Newbie
    Currently Being Moderated
    Hi gimbal2,

    Thank you very much.


    I got your answer but still I can't understand what is the need of "protected" constructor...???

    example like...


    public class Animal {

    private String name;

    protected Animal(String name){
    this.name = name;
    }

    public String getName() {
         return name;
    }

    public static Animal createMonkey(){
    return new Animal("monkey");
    }

    public static Animal createBear(){
    return new Animal("bear");
    }
    }



    public class MainClass {

    public static void main(String[] args){

    Animal horse= new Animal("Horse");
              System.out.println(horse.getName());
              Animal monkey= Animal.createMonkey();
              System.out.println(monkey.getName());
    }
    }


    Note:both the class are in the same package.


    In MainClass both monkey and horse objects are created with same access modifier constructor.


    please solve my doubt.
  • 3. Re: Java OO design
    gimbal2 Guru
    Currently Being Moderated
    EDIT:

    Hur, I'm a bit stumped there. I would assume you cannot instantiate the animal with a protected constructor, but a quick test turns out that you can in fact if the classes are in the same package. If the classes are in a different package then it is not possible.

    It makes sense in a way actually. Creating protected constructors allows you to create classes that are public within their own package, but 'invisible' to classes outside of the package. Didn't realize this was possible, very useful now that I know it exists.
  • 4. Re: Java OO design
    796440 Guru
    Currently Being Moderated
    908259 wrote:
    Hi gimbal2,

    Thank you very much.


    I got your answer but still I can't understand what is the need of "protected" constructor...???
    They're useful in abstract classes. The concrete subclass has to call some parent constructor, either implicitly or explicitly. There's no reason to expose them to the public, since they can't be used other than by subclasses anyway.

    EDIT: And in fact, not only in abstract classes (though that's the most common use), but in general, if you want a constructor to be invoked by a subclass, but not by the world in general, you make it protected. Exactly the same reason as you make anything else protected.

    Edited by: jverd on Jan 16, 2012 10:14 AM
  • 5. Re: Java OO design
    911262 Newbie
    Currently Being Moderated
    Hello gimbal2,

    Animal class in javaclass package having protected constructor and MainClass is in different package which extends Animal.

    Here I defined protected Animal constructor but it will gives error in different package while creating any object even I extends Animal class.

    protected keyword is associated with inheritance and we know that constructors are never inherited.

    Now my question is "what is the need of protected constructor..???"

    ===========================
    package javaclass;

    public class Animal {

    private String name;

    public Animal() {
    }

    protected Animal(String name){
    this.name = name;
    }

    public String getName() {
         return name;
    }

    public static Animal createMonkey(){
    return new Animal("monkey");
    }

    public static Animal createBear(){
    return new Animal("bear");
    }
    }

    ===========================

    import javaclass.*;

    public class MainClass extends Animal{
    public static void main(String[] args){
              Animal horse= new Animal("Horse");
              System.out.println(horse.getName());
    }
    }

    ==============================

    Output gives ERROR :

    Second.java:5: Animal(java.lang.String) has protected access in javaclass.Animal

    Animal horse= new Animal("Horse");

    Edited by: 908259 on Jan 16, 2012 7:17 PM
  • 6. Re: Java OO design
    796440 Guru
    Currently Being Moderated
    908259 wrote:
    protected keyword is associated with inheritance and we know that constructors are never inherited.
    More specifically, protected is associated with making something in the parent class accessible to the child but not to the whole world. Even though constructors aren't inherited, parent's constructors can still be invoked from a child class.
    Now my question is "what is the need of protected constructor..???"
    See my previous post.
  • 7. Re: Java OO design
    gimbal2 Guru
    Currently Being Moderated
    jverd wrote:
    >
    See my previous post.
    Exactly that. A simple example:
    public class Horse extends Animal {
      public Horse(){
        super("horse"); // Horse class is allowed to invoke protected constructor of parent class
      }
    }
    And since the constructor of Horse is public, you can create instances of it.

    Please don't go using these simplified examples as "design patterns". Yes they work, but that doesn't mean these are good ideas. But hopefully when the time comes, you'll learn how to properly use protected to design a piece of code, or you know not to use it (even more important IMO).
  • 8. Re: Java OO design
    911262 Newbie
    Currently Being Moderated
    Okay I got it.

    Thanks.
  • 9. Re: Java OO design
    911262 Newbie
    Currently Being Moderated
    Okay I got it.

    Thanks.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points