anyone can explain to me what is the concept of inheritance and access modifier. actually, i can't understand how it be used in java program

10 Years
Discussion Span
Last Post by PoovenM

So inheritance is huge concept in Object Orientated Programming. It allows us to create objects in a very clean manner. It's best explained with an example.
Suppose that we have a class Animal. This class would have attributes that most animal’s posses, such as number of legs, stable food, average weight, etc. Now lets say we have a pet shop and they store some of this information, including price which is accessed via the method price().
We can then create subclasses, i.e. inherit from Animal so that we have Cat, Dog and Hamster.
Now suppose that we need to be able to create one of these animals based on user input, and that the actions performed on each animal is the same. Such as getting the price of the animal and display the facts we currently have.
The problem with:

Cat sale = new Cat()

is that now we must have different processing algorithms for the Cat, Dog and Hamster even though we want to call a method each have, say price() and display().
The solution inheritance offers is:

Animal sale = new Cat();

now sale can be any one of the Cat, Dog or Hamster and the method price() can be called on it and the respective subclasses method will be evoked.
The limitation here is that the method you call must be defined in Animal. So if Cat has a unique method say, meow() then it will not be accessible via sales.
Okay so to reaffirm. the class Animal has methods and attribute that all subclasses will now have. It's often that Animal would be an abstract class (which basically means it's empty but has the methods that we want all our subclasses to have). We overwrite the methods of our super class (i.e. Animal) in our subclasses so that each subclass has the same method name but the actions they carry our vary.

That's inheritance in a nut shell. Now for the concept of access modifies... well I'm guessing that refers to: public, private and protected.

class A
  private int num;
  protected String name;
  public void doAction()
      System.out.println("Action done.");
class B extends A
   public B()
      name = "Pooven";
class C
  public static void main(String[]args)
     A objectA = new A();

Now since doAction is marked public, when we create an object from the class, that method is accessible to everyone. It is also accessible to any of its subclasses.
Since name is protected, it will only be accessible to a subclass and not any objects that we create from it, so objectA.name = "Pooven"; would give a compile error. But accessing name in class B (the inherited class) is fine.
Since num is declared private, it can only be accessed within class A. So objectA.num would give a compile error, and so would trying to access it in class B.

Hope that brings some light to the topic. Peace

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.