Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

  • Re: General CS concepts

    Inheritance

    The concept of inheritance is similar to inheritance in real life: the child gets what the parent has. Also, the child still has what the child had, and can more-or-less do what it wants with what the parent had.

    In computer science, there are some limitations. Remember declaring things private or protected, and thinking to yourself "these two keywords look like they're doing the same thing!"? Inheritance is why they're there. Private things are not given to the child, but protected and public are. There's not really a good analogy that I can think of for this, but hopefully it's not too hard to remember.

    Another thing to keep in mind is that inheriting classes can override the methods of their parent class. An analogy could be say you have a Fararri. You inherit a Ford Cobalt. Which are you going to keep/use? Hopefully you said Fararri, because it's yours. In Java, the inheriting class always uses it's own methods/fields whenever possible. This is unlike real life, where we consider the opposite situation: you have an old 1980 pickup truck, and you inherit a Porshe 911. If you worked like Java did, you would keep using the 1980 pickup truck

    So, let's inherit some things! Java's keyword for inheritance is extends, which makes sense because you are "extending" the capability of the parent class. In Java, it is important to note that you can ONLY inherit from one source. However, that source can inherit from another parent, which in turn can inherit from its parent, and so on and so forth so long as each only inherits from one source. Note that EVERYTHING in java inherits from the class Object.

    public class A
    {
         public int a;
         public A()
         {
              a = 5;
         }
     
         public int DoIt1()
         {
              return 2*a+DoIt2();
         }
     
         protected DoIt3()
         {
              return 4*a;
         }
     
         private int DoIt2()
         {
              return a;
         }
    }

    public class B extends A
    {
         public B()
         {
              super();
         }
     
         public int doIt4()
         {
              return doIt1()+doIt3()+3;
         }
     
         public int doIt1()
         {
              return a;
         }
    }

    So, what will happen when we call doIt1 from B? It should return a, because we had overriden the doIt1 method in A.

    What if you tried calling doIt4 from B? It would return a+4*a+3, because the doIt1 was overrided, but doIt3 was inherited from A.

    The last concept I'll put is the super concept. I used it above, so what does it mean? This is a tricky way to say you want to use the Porshe 911: you're using the parent's method There are some limits on this:
    * If in the constructor, the super method MUST be the first line if it's used. Also, if your parent class doesn't have a constructor that can be called by super() (no parameters), you MUST specify the super constructor.
    * Inside other methods, the super method can ONLY be used to specify the method that it is over-riding, not any method in the parent class.
    * I'm assuming here that the super can be used anywhere in other methods, but don't take my word for it.

    Here are some simple questions about inheritance:

    1. What is a super class?

    2. What is a super constructor? How is it different from a regular constructor?

    3. What does a class inherit from it's parents? What does it not inherit?

    Answers are coming later

    Happy coding
    This article was originally published in forum thread: General CS concepts started by helloworld922 View original post