The Sense of Inheritence and Polymorphism is quite simple, first forget everything from you mind and consider the following.
class Employee
{
/*common attributes in the super class, which will be inherited in the child class
*/
int socialSecurityNumber = 0;
Srring firstName, lastName;
//this method is implemented, note that it has the definition
//we have also the code in method body, because, we've know
//already that every Employee can speak the same
public void speak()
{
System.out.println("I'm the Employee, Hahahaha, What is problem");
}
//this method is abstract and we have not the parenthesis because,
//we don't have information about its implementation, so, every
//class that is extended from it should have to implement that
public abstract void work();
}
class Clerk extends Employee
{
public void work()
{
doAccounting();
makeRecords();
}
}
class Manager extends Manager
{
public void work()
{
arrangeMeetings();
manageWorks();
manageEmployees();
}
}
1. A Super Class is a class that has the most general form of information, like consider you are creating an application of Employees which has different designation you know that in general they will be employees so create a class 'Employee' which is a super class of other designation. it may contains the fields (attributes / data members) like firstName, lastName, socialSecurityNumber etc ... . then other classes like Clerk, Manager will extend this information because we don't want to re-write the all code and don't want to define these data members again in the system using this.
2. Keep one thing in mind, that, it is not necessary to define methods in super class only. but it depends on the situation for example, if you know already in advance that sub classes will contain these specific methods that are common in all then you define it in super class and suppose if the implementation is also the same for example a method of speak() which is off course will be same because, every Employee will be able to speak surely, here you are in position to also to implement the method (coding in the class).
3. if you realize that each child has a different implementation of some method, means, they do a bit different, you then only define in the super class and making this super class abstract and then, in each sub class you override this method. Example is a work() method, which will be different for each Employee since every Employee is supposed to work differently.
4. Casting is a machenisms to support the flexibility in Object Oriented. As you know that if different subclasses could be referrred using the Name of the Super class. For example, you can store the reference of Clerk and Manager object in Employee. see the following code snippet
public static void main(String[] args)
{
Employee emp1 = new Clerk();
Employee emp2 = new Manager();
}
the benifits is as we can manipulate every type of sub class object in same way as other. when we need the specific functionality then we cast it as its nature. consider the two following ways
emp1.speak (); //valid because it is common
emp1.work (); invalid because actually employees work is not defined, so the correct one is
(Clerk)emp1.work();
5. instanceof is used to determine that either the object belongs to specific to that class. it returns true if, false otherwise
if(emp1 instanceof Clerk())
return true;
else
return false;
it will return true because see the previous code than it.
|