Saturday, March 5, 2016

Day 3 Object Oriented

Inheritance

The process by which one class acquires the properties and functionalities of another class. Inheritance provides the idea of reusability of code and each sub class defines only those features that are unique to it.
  1. Inheritance is a mechanism of defining a new class based on an existing class.
  2. Inheritance enables reuse of code. Inheritance also provides scope for refinement of the existing class. Inheritance helps in specialization
  3. The existing (or original) class is called the base class or super class orparent class. The new class which inherits from the base class is called the derived class or sub class or child class.
  4. Inheritance implements the “Is-A” or “Kind Of/ Has-A” relationship.
Note : The biggest advantage of Inheritance is that, code in base class need not be rewritten in the derived class.
The member variables and methods of the base class can be used in thederived class as well.
Inheritance Example
Consider below two classes –
Class Teacher:
class Teacher {
   private String name;
   private double salary;
   private String subject;
   public Teacher (String tname)  {
       name = tname;
   }
   public String getName()  {
       return name;
   }
   private double getSalary()  {
       return salary;
   }
   private String  getSubject()  {
        return  subject;
   }
}
Class: OfficeStaff
class  OfficeStaff{
   private String name;
   private double salary;
   private String dept;
   public OfficeStaff (String sname)  {
      name = sname;
   }
   public String getName()  {
       return name;
   }
   private double  getSalary()  {
       return salary;
   }
   private String  getDept ()  {
       return dept;
   }
}
Points:
1) Both the classes share few common properties and methods. Thus repetition of code.
2) Creating a class which contains the common methods and properties.
3) The classes Teacher and OfficeStaff can inherit the all the common properties and methods from below Employee class
class Employee{
   private String name;
   private double salary;
   public Employee(String ename){
      name=ename;
   }
   public String getName(){
      return name;
   }
   private double getSalary(){
      return salary;
   } 
}
4) Add individual methods and properties to it Once we have created a super class that defines the attributes common to a set of objects, it can be used to create any number of more specific subclasses
5) Any similar classes like Engineer, Principal can be generated as subclasses from the Employee class.
6) The parent class is termed super class and the inherited class is the sub class
7) A sub class is the specialized version of a super class – It inherits all of the instance variables and methods defined by the super class and adds its own, unique elements.
8) Although a sub class includes all of the members of its super class it can not access those members of the super class that have been declared as private.
9) A reference variable of a super class can be assigned to a reference to any sub class derived from that super class
i.e. Employee emp = new Teacher();
Note: Multi-level inheritance is allowed in Java but not multiple inheritance
multilevel and multiple inheritance diagram representation, Object oriented programming concepts
Types of Inheritance
Multilevel Inheritance
Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class.
Multiple Inheritance
Multiple Inheritance” refers to the concept of one class inheriting from more than one base class. The inheritance we learnt earlier had the concept of one base class or parent. The problem with “multiple inheritance” is that the derived class will have to manage the dependency on two base classes.
Note 1Multiple Inheritance is very rarely used in software projects. Using Multiple inheritance often leads to problems in the hierarchy. This results in unwanted complexity when further extending the class.
Note 2: Most of the new OO languages like Small Talk, Java, C# do not support Multiple inheritance. Multiple Inheritance is supported in C++.

Polymorphism

Polymorphism is a feature that allows one interface to be used for a general class of actions. It’s an operation may exhibit different behavior in different instances. The behavior depends on the types of data used in the operation. It plays an important role in allowing objects having different internal structures to share the same external interface. Polymorphism is extensively used in implementing inheritance.
Types of Polymorphism
1) Static Polymorphism
2) Dynamic Polymorphism
Static Polymorphism:
Function Overloading – within same class more than one method having same name but differing in signature.
Resolved during compilation time.
Return type is not part of method signature.
Dynamic Polymorphism
Function Overriding – keeping the signature and return type same, method in the base class is redefined in the derived class.
Resolved during run time.
Which method to be invoked is decided by the object that the reference points to and not by the type of the reference.
Overriding:
Redefining a super class method in a sub class is called method overriding.
The method signature ie. method name, parameter list and return type have to match exactly.
The overridden method can widen the accessibility but not narrow it, ie if it is private in the base class, the child class can make it public but not vice versa.
Overriding Examples:
Consider a Super Class Doctor and Subclass Surgeon. Class Doctor has a method treatPatient(). Surgeon overrides treatPatient method ie gives a new definition to the method.
Doctor doctorObj = new Doctor();
// Call the treatPatient method of Doctor class
doctorObj.treatPatient() 
Surgeon surgeonObj = new Surgeon();
// Call the treatPatient method of Surgeon class
surgeonObj.treatPatient()
Doctor obj = new Surgeon();
// calls Surgeon’s treatPatient method as the reference is pointing to Surgeon
obj.treatPatient();
Method/Function Overloading:
Method Overloading refers to the practice of using the same name to denote several different operations. Overloading can be done for both functions as well as operators. Here we look at only Method overloading.
Declaration:
void SomeMethod (int value);
void SomeMethod (float value);
void SomeMethod (char value);
void SomeMethod (String* str);
void SomeMethod (char* str);
All the five methods are called ‘SomeMethod ’. All the methods have the same name, but different signatures.
The concept of the same function name with different types of parameters being passed is called Function Overloading.
1) In Overloading we can reuse the same method name by changing the arguments.
2) Overloaded methods- Must and Must Not Facts:
  • The Overloaded method must have different argument lists,
  • Can have different return types but in that case it is mandatory to have different argument list.
  • Can have different access modifiers and
  • Can throw different exceptions
3) Methods can be overloaded in the same as well as the sub classes.
Q: What determines which overridden method is used at runtime?
A: Object type
Q: What determines which overloaded method will be used at compile time?
A: Reference type determines. Operator overloading refers to the operators like ‘+’ being used for different purposes based on the data type on either side of the operator.

Combined example for Inheritance & Polymorphism

abstract public class Employee {
   private String name;
   public Employee(String ename) { 
      name = ename;
   }
   public String getName() {
      return new name;
   }
   private void setName(String name) {
      this.name = new String(name);
   }
   abstract public double pay();
   public String toString() {
       return "name is" + name;
   }
}
public class Salaried extends Employee {
   double salary;
   public Salaried(String name, double s) {
      super(name);
      salary =s;
   }
   public void setSalary(double salary) {
      this.salary = salary;
   }
   public double getSalary() {
      return salary;
   }
   public double pay() {
      return salary;
   }
   public String toString(){
      return super.toString() + " (salary is " +salary+")"; 
   }
}

IS-A & HAS-A Relationships

public class SuperClass {  }
public class SubClass1 extends SuperClass { //SubClass 1 code goes here }
public class SubClass2 extends SubClass1 { //SubClass2 Specific code goes here }
HAS-A relationships are based on usage, rather than inheritance. In other words, class A HAS-A B if code in class A has a reference to an instance of class B.
For example, we can say the following,
A Car IS-A Vehicle. A Car HAS-A License. and the code looks like this:
public class Vehicle{ }
public class Car extends Vehicle{
   private License myCarLicense;
}
  1. JAVA TRAINING IN KATHMANDU
  2. JAVA TRAINING IN LALITPUR

No comments:

Post a Comment