Solved programming problem

Basic Abstract-1 in Java.

Source code

Main Class:

 
package abstruct_1;

public class Abstruct_1

{

    public static void main(String[] args)

    {  

     //   Animal A = new Animal();///Not possible

     

        Animal a = new Bird();//// sub class polimerphysom

        a.eat();

        a.sleep();

        a.makeSound();

        ///for call fly method we have to use casting

        ((Bird)a).fly();

    }

 

}

 

Animal Class:

 

package abstruct_1;

 

public abstract class Animal  ///parent + abstruct class

{

    String name,color;

    double weight;

    

   Animal()

    {

        /// nothing to do

    }

    Animal(String Name,String Color)

    {

        this.name = name;

        this.color = color;

        

    }

    

     Animal(String Name,String Color,double weight)

    {

        this.name = name;

        this.color = color;

        this.weight = weight;      

    }

     

     public void eat()

     {

         System.out.println("Animal Eats");

     }

     

    ///abstract method.... those to must be override in child class 

    public abstract void makeSound();

    public abstract void sleep();

}

 

Bird Class:

 

 

package abstruct_1;

 

public class Bird extends Animal

{   

    public void eat()

    {

        System.out.println("Bird Eats");

    }

 

    @Override

    public void makeSound() 

    {

        System.out.println("Chirp");

    }

 

    @Override

    public void sleep() 

    {       

       System.out.println("Bird Sleep");

    }

    

    public void fly()

    {

        System.out.println("Bird Fly");

    }

}

Output

Bird Eats

Bird Sleep

Chirp

Bird Fly

Join the discussion

Add your comment