Powered by Blogger.
Showing posts with label definition of inheritance. Show all posts
Showing posts with label definition of inheritance. Show all posts

Best Example of Encapsulation, and Inheritance in Java

Overriding Methods:-

If a method is defined in a sub-class so that the name, return type, and argument list must exactly those of a method in the parent class, then the new method is said to override the old one. The overriding method can not be less accessible than the method it overrides.

Overloading Methods:-

We can declare several methods of same name in a class, which is known as methods overloading. For overloading methods argument lists must be different and return type can be different. In the following code, there are four methods of same name with different argument, which is an example of overloading methods.

Invoking an Overridden Method:-

The parent keyword can be used with any method that overrides its counterpart in a parent class. When you override a method, you may not wish to obliterate the functionality of the parent but rather extend it. You can achieve this by calling the parent class’s method in the current object’s context. Let’s explore two examples which will clear the concept of obliteration and extension of parent method.

Invoking Overridden Super-class Methods:-

Sometimes within a class you need to invoke an overridden superclass method instead of invoking a method that has the same signature and is defined in the current class.

For example, suppose that the CheckingAccount class overrides the debit instance method defined in its immediate superclass, Account. You could invoke the Account debit method within a method in the CheckingAccount class by coding this statement:

Example with overriding, overloading, and Invoking:

 class Man{
   private int weight;

   public Man(int weight){
      this.weight=weight;
   }

   public void setWeight(int weight){   // seter method
      if(weight>=50 && weight<=100){
         this.weight=weight;
      }
   }

   public int getWeight(){   // geter method
      return weight;
   }

   public void show(){
      System.out.println(weight);
   }
  }

  public class SuperMan extends Man{
    public int power;

    public SuperMan(int power){
       super(55);    // invoking parent class constructor
       this.power=power;
    }

    public SuperMan(int weight,int power){    //Over loading
       super(weight);     // invoking parent class constructor
       this.power=power;
    }

    public void show(){     // overriding method
       super.show();          // invoking parent class method
       System.out.println(power);
    }

    public void show(String abc){     // overloading method
       System.out.println(abc);
    }

    public static void main(String[]args){
       SuperMan s = new SuperMan(10);
       s.show();
       SuperMan s1 = new SuperMan(50,100);
       s1.show(”Tiger”);
    }
  }

Java - Definition of Inheritance Object Oriented Programming

Definition of Inheritance:-

Inheritance is the process of sub-classing that we can create a child-class from a parent-class. Java programming language permits single inheritance, because in Java a class can extend one other class only. A child-class can inherited all of the members from the parent-class, but it does not inherit the constructor.

Subclasses and Superclasses:-

A subclass, heir class, or child class is a modular, derivative class that inherits one or more properties from another class (called the superclass, base class, or parent class). The properties in question vary from language to language, but commonly include class data variables, properties, and methods or functions. Some languages support the inheritance of other properties as well. For example, in Eiffel, contracts which define the specification of a class are also inherited by heirs. The superclass establishes a common interface and foundational functionality, which specialized subclasses can inherit, modify, and supplement. The software inherited by a subclass is considered reused in the subclass.

In some cases, a subclass may customize or redefine a method inherited from the superclass. A superclass method which can be redefined in this way is called a virtual method.

Roles and Inheritance:-

Sometimes inheritance-based design is used instead of roles. A role, such as a Student role of a Person describes a characteristic associated with the object that exists because the object happens to participate in some relationship with another object; for example, the person in the student role has an "enrolled" relationship with a Course object. Some object-oriented design methods do not distinguish this use of roles from more stable aspects of objects. Thus there is a tendency to use inheritance to model roles; a Student role of a Person would be modeled as a subclass of a Person. However, neither the inheritance hierarchy nor the types of the objects can change with time.

Therefore, modeling roles as subclasses can cause roles to be fixed on creation; a Person cannot easily change his role from Student to Employee when circumstances change. From a modeling point of view, such restrictions are often not desirable, because this causes artificial restrictions on future extensibility of the object system, making future changes harder to implement because existing design needs to be updated.

Inheritance is often better used with a generalization mindset, such that common aspects of instantiable classes are factored to superclasses, for example, having a common superclass 'LegalEntity' for both Person and Company classes for all the common aspects of both. The distinction between role based design and inheritance based design can be made based on the stability of the aspect. Role based design should be used when it is conceivable that the same object participates in different roles at different times, and inheritance based design should be used when the common aspects of multiple classes are factored as superclasses, and do not change with time.

One consequence of separation of roles and superclasses is that this cleanly separates compile-time and run-time aspects of the object system. Inheritance is then clearly a compile-time construct. It does influence the structure of many objects at run-time, but the different kinds of structure that can be used are already fixed at compile-time.

To model the example of Person as an employee with this method, the modeling ensures that a Person class can only contain operations or data that are common to every Person instance regardless of where they are used. This would prevent use of a Job member in a Person class, because every person does not have a job, or at least it is not known that the Person class is only used to model Person instances that have a job. Instead, object-oriented design would consider some subset of all person objects to be in an "employee" role. The job information would be associated only with objects that have the employee role. Object-oriented design would also model the "job" as a role, since a job can be restricted in time, and therefore is not a stable basis for modeling a class. The corresponding stable concept is either "WorkPlace" or just "Work" depending on which concept is meant. Thus, from an object-oriented design point of view, there would be a "Person" class and a "WorkPlace" class, which are related by a many-to-many associatation "works-in", such that an instance of a Person is in employee role, when he works-in a job, where a job is a role of his work place in the situation when the employee works in it.

Note that in this approach, all of the classes that are produced by this design process form part of the same domain, that is, they describe things clearly using just one terminology. This is often not true for other approaches.

The difference between roles and classes is unclear if one assumes referential transparency because roles are types of references and classes are types of the referred-to objects.

Inheritance Program:-

 class Cat{
   public int x = 10;
   public int y = 20;

   public void show(){
     System.out.println(x+” ”+y);
   }
 }

 public class Rat extends Cat{
    public int z = 30;

    public static void main(String[]args){
       Rat r = new Rat();
       r.show();
       System.out.println(r.x+” ”+r.y+” ”+r.z);
   }
 }

In the above code, Rat class extends Cat class, so Rat class become child class of Cat class.