abstract class in java

abstract class in java

Main motive to make the abstract class in java to make the standard . Standard always created by the developer and implemented by the programmer.

Let’s take an example:

class Bird
{
    public void flying();
}

Here we have created one standard with the name of flying but when we will make undefined function then function shouting…… either define me or declared as an abstract so we want to make standard then we will write abstract in front of the function.

class Bird
{
  abstract public void flying();
}

 

Now function don’t have any problem but normal class cannot contain undefined function or abstract function here class shouting either define the flying function or make class abstract.

  • Normal class cannot contain undefined function
  • Abstract class can contain defined and undefined function

Here you can see it would easily compile:

abstract class Bird
{
    abstract public void flying();
}

Also we can make defined function in the abstract class see here:

abstract class Bird
{

                abstract public void flying();
                public void breath()
                {
                                System.out.println("Birds Breathing");
                }

}

when we will compile the above program then no error will come see below screenshot:

abstract class in java

 

Implementation of the abstract class:

  • When we will make the standard in the java then programmer will implement the standard same here we will make another class there we will extend the abstract class and define the standard according to the requirement.

See in the example:

abstract class Bird
{
	abstract public void flying();
	
	public void breath()
	{
		System.out.println("Birds Breathing");
	}
}

class Sparrow extends Bird
{
	public void flying()
	{
		System.out.println("Sparrow is flying according to own strength");
	}
}

class Eagle extends Bird
{
	public void flying()
	{
		System.out.println("Eagle is flying according to own strength")
	}
}

In the Sparrow class we extend the Bird abstract class using the extends keyword in java. when we will extend the abstract class then there would be same scenario like normal Sparrow class cannot contain undefined function either We need to make Sparrow class the abstract class or need to define the abstract function and in the Sparrow class we have defined abstract function flying.

abstract class in java
abstract class in java

 

abstract class Bird
{
	abstract public void flying();
	
	public void breath()
	{
		System.out.println("Birds Breating");
	}
}

class Eagle extends Bird
{
	public void flying()
	{
		System.out.println("Eagle is flying according to own strength");
	}
	public static void main(String args[])
	{
		Eagle e1=new Eagle();
		e1.flying();
	}
	
}

Here above program will easily compile and execute:

Output of the above program: 

Eagle is flying according to own strength

  • We cannot instantiate the object of the abstract class 

In the above program 1 thing is missing Eagle class define the function and and we execute the flying function from the Eagle class object then what’s the benefit to make the abstract class here……

what exactly meaning of abstract class:

a) Developer will make the standard

b) Programmer will implement the standard 

c) After Developer will execute the standard

In the above example programmer implement the standard and programmer also execute the standard so we will make it simple.

See in this example:

abstract class Bird
{
	abstract public void flying();
	
	public void breath(Bird b)
	{
		b.flying();
	}
}

class Eagle extends Bird
{
	public void flying()
	{
		System.out.println("Eagle is flyig according to own strength");
	}
	public static void main(String args[])
	{
		Eagle e1=new Eagle();
		e1.breath(e1);
	}
	
}

Hope you like this tutorial and if you have any question then let me know!!!!!!!!!!! Thanks

Download PDF

37 thoughts on “abstract class in java

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. I like the helpful info you provide in your articles.

    I’ll bookmark your blog and check again here regularly.
    I am quite sure I will learn a lot of new stuff right here!
    Good luck for the next!

  2. My brother suggested I might like this website. He was totally right. This post truly made my day. You cann’t imagine just how a lot time I had spent for this information! Thanks!

  3. you’re actually a just right webmaster. The web site loading velocity is amazing. It sort of feels that you are doing any distinctive trick. In addition, The contents are masterpiece. you have done a magnificent job on this topic!

  4. I absolutely love your website.. Excellent colors & theme. Did you make this web site yourself? Please reply back as I’m hoping to create my own personal blog and want to know where you got this from or exactly what the theme is named. Many thanks!

  5. I don’t even know how I ended up here, but I thought this post was good. I do not know who you are but definitely you are going to a famous blogger if you are not already 😉 Cheers!

  6. The other day, while I was at work, my sister stole my apple ipad and tested to see if it can survive a forty foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has 83 views. I know this is entirely off topic but I had to share it with someone!

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.