The capability of using inheritance saves on the coding needed and ensures bette
ID: 657678 • Letter: T
Question
The capability of using inheritance saves on the coding needed and ensures better quality assurance.
Write some code that can help demonstrate your support or disagreement with the above statement.
OR
There is no value to using polymorphism.
Write some code that can help demonstrate your support or disagreement with the above statement.
OR
The capability of overloading a method adds confusion to the development of program code developed by a programmer.
Write some code that can help demonstrate your support or disagreement with the above statement.
Explanation / Answer
Inheritance:
import java.util.*;
public class Employee
{
private String name;
private double eSalary;
private Date dateOfJoin;
public Employee(String n, double s, int year, int month, int day)
{
name = n;
eSalary = s;
GregorianDoj doj = new GregorianDoj(year, month - 1, day);
dateOfJoin = doj.getTime();
}
public String getName()
{
return name;
}
public double getESalary()
{
return eSalary;
}
public Date dateOfJoin()
{
return dateOfJoin;
}
public void IncreaseESalary(double byPercent)
{
double raise = eSalary * byPercent / 100;
eSalary += raise;
}
}
public class Manager extends Employee
{
String dept;
double bonus;
public Manager(String n, double s, int year, int month, int day,String dep)
{
super(n, s, year, month, day);
dept=dep;
}
public String getDepartment()
{
return dept;
}
public void setBonus(double b)
{
bonus=b;
}
public static void main(String[] args)
{
// construct a Manager instance
Manager mang = new Manager("Joy david", 90000, 1997, 14, 14,"Finace");
mang.setBonus(5000);
// print out information about manger
System.out.println("name=" + mang.getName() + ",eSalary=" + mang.getESalary());
}
}
Here, Employee is a common class defines an employee in a company. Manager is also an employee of a company, but a manger has additional properties or rights than a general employee. Through inheritance, a sub class acquires the properties of super class and can define its own properties. By inheriting a class (i.e. code reuse) the code and time can be saved.
Hence,
The capability of using inheritance saves on the coding needed and ensures better quality assurance.
Plymorphism:
class SuperClass
{
public void methodToOverride() //Base class method
{
System.out.println ("I'm the method of SuperClass");
}
}
class SubClass extends SuperClass
{
public void methodToOverride() //Derived Class method
{
System.out.println ("I'm the method of SubClass");
}
}
public class TestMethod
{
public static void main (String args []) {
// SuperClass reference and object
SuperClass obj1 = new SuperClass();
// SuperClass reference but SubClass object
SuperClass obj2 = new SubClass();
// Calls the method from SuperClass class
obj1.methodToOverride();
//Calls the method from SubClass class
obj2.methodToOverride();
//assigning sub class object to Super class object
obj1=obj2;
//Calls the method from subclass class
obj1.methodToOverride();
}
}
Due to Polymorphism ,an object can take on many forms. Polymorphism makes code easier. Therefore, polymorphism is a very useful in object oriented programming.
Hence, there is a value to using polymorphism.
Method overloading:
public class AddAny2
{
void sum(int a,int b)
{
System.out.println(a+b);
}
void sum(double a,double b)
{
System.out.println(a+b);
}
void sum(int a, double b)
{
System.out.println(a+b);
}
void sum(String a, String b)
{
System.out.println(a+" "+b);
}
public static void main(String args[])
{
AddAny2 obj=new AddAny2();
obj.sum(10.5,10.5);
obj.sum(20,20);
obj.sum(20,20.5);
obj.sum("James","Philip");
}
}
This is the simple addition class, which implements sum () method 4 times with different signature. In method overloading, signature makes the difference. Therefore no confusion will occur.
Hence,
The capability of overloading a method never adds confusion to the development of program code developed by a programmer.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.