Blog
Part 1
Part 1:
Inheritance is the object oriented concept which enables to implement real world inheritance behavior. It enables the user to inherit the properties and behavior and facilitate the reusability of the code and overriding the methods. It is implemented by using “extends and implements” keyword where child class extends the behavior of the parent class. The inheritance helps in establishing the relationship between two classes also referred as super class and sub class. Moreover, a super class may have several sub classes. This property of OOPS makes code more flexible whereby changes can be implemented only in parent class and its property will automatically be inherited in child class.
For example – The following example shows that employee is a super class and programmer is a sub class, where class programmer can access the behavior of the emp class.
class emp{
int sal = 40000;
}
class programmer extends emp{
int incentive = 10000;
Public static void main(String args[])
{
Programmer a = new Programmer();
system.out.println(“Salary of Programmer is :” + a.sal);
system.out.println(“Additional incentive is :” + a.incentive);
}
}
Part 2:
Abstraction is the ability to make class abstract which cannot be instantiated. Abstract class is something which is not concrete and is incomplete. We can take the example of the remote control, where we can control the device with the given button and we do not take care of the circuits in the remote, these are irrelevant. In Java, this concept can be applied as follows:
abstract class SoupFactory
{
String factLoc;
public String getFactLocation()
{
return factLoc;
}
public ChickenS makeChickenS()
{
return new ChickenS();
}
public CChowder makeCChowder()
{
return new CChowder();
}
public FishChowder makeFishChowder()
{
return new FishChowder();
}
public Mstrone makeMstrone()
{
return new Mstrone();
}
public PastaFazul makePastaFazul()
{
return new PastaFazul();
}
public TofuS makeTofuS()
{
return new TofuS();
}
public VegSoup makeVegSoup()
{
return new VegSoup();
}
}
An object can be defined as “SoupFactory”, and instantiated by any soup factory either HonoluluConcreteSoupFactory or BostonConcreteSoupFactory. Both soup factories have the “makeFishChowder” method, and both return a “FishChowder” type class. However, the HCSF returns a “FishChowder” subclass of “HonoluluFishChowder”, while the BCSF returns a “FishChowder” subclass of “BostonFishChowder”. Thus, the abstract class “soupFactory” is used by any of the soup factories
