Wednesday 29 August 2012

What is Generalization in Java


> Generalization is the process in which
> we extract common-shared behavior of two or more classes,
> and combine them into a generalized Super Class 


For example:

class dog
{
    void fourLegs()
    {
        System.out.println("four legged animal");
    }
    void bark()
    {
        System.out.println("Bark");
    }
}
class cat

{
    void fourLegs()
    {
        System.out.println("four legged animal");
    }
    void Mew()
    {
        System.out.println("Bark");
    }
}




After Generalization (Shared characterstics are all together)

abstract class Animal
{
    void fourLegs()
    {
        System.out.println("four legged animal");
    } 
}

class dog extends Animal
{
    void bark()
    {
        System.out.println("Bark");
    }
}
class cat extends Animal

{    void Mew()
    {
        System.out.println("Bark");
    }
}


No comments:

Post a Comment