Abstract Class and Abstraction in Java.

We are going to discuss the abstract class. Before learning the Java abstract class, we are going to discuss the Abstraction and Abstract method.

 

What is Abstraction in Java? 

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

The main purpose of abstraction is to hide the unnecessary details from the users.

I another way, we can say abstraction is shown only essential things to the user and hides the internal details from the user.

Let’s try to understand with an example, we are withdrawing money from ATM, so we are entering the ATM card and pin number and amount and you get the money, but we don’t know what is internally going on in this process.

so in this process, we are only able to see essential things like entering a password, enter amount, but we are not able to see the internal process of that transaction.

 

How to achieve Abstraction in java.

There are two ways to achieve abstraction in java.

  • Abstract class (0 to 100%)
  • Interface (100%)

 

When we are using Abstract class then we are not able to achieve 100% abstraction. Because in Abstract class we are using the abstract method and non-abstract method also.

When we are using Interface than we are able to achieve 100% abstraction. Because in Interface we are using the only abstract method. But after Java 8 we are able to non-abstract method also.

 

What is Abstract method?

If you want to write our method but you don’t know the definition of the method you just know the name of the method and the number of parameters it will take, so what we can do is we can declare the methods just write the method name, parameters, and a semicolon.

If you declare a method in Java-like (methods without body) that method directly becomes abstract so those methods are called abstract methods.

An abstract method is a method that has just the method definition but does not contain implementation.

abstract void goingTo();//no method body and abstract 

 

What is Abstract class?

A class that is declared using the “abstract” keyword is known as an abstract class.

An abstract class can have abstract methods (“methods without a body”), and also concrete methods (“methods with the body”).

In Abstract class, you can not get the object, so the first point to remember for abstract classes you cannot create an object of an abstract class.

An abstract class can have abstract methods it’s doesn’t mean you cannot have an abstract class without abstract methods.

abstract class Example{} 

 

Java program of Abstract class that has an abstract method

//abstract Class 
abstract class Car {
	abstract void run(); // no method body and abstract
}

class Maruti extends Car {
	void run() { //implementing abstract method
		System.out.println("running safely");
	}

	public static void main(String args[]) {
		Car obj = new Maruti();
		obj.run();
	}
}

Output: 

running safely


abstract class Game { abstract void play(); } //In real scenario, implementation is provided by others i.e. unknown by end user class Cricket extends Game { void play() { System.out.println("playing Cricket"); } } class FootBall extends Game { void play() { System.out.println("playing FootBall"); } } //In real scenario, method is called by programmer or user class ExampleAbstraction { public static void main(String args[]) { Game objCricket = new Cricket(); // creating Cricket class object objCricket.play(); Game objFootBall = new FootBall(); // creating Cricket class object objFootBall.play(); } }

Output:

playing Cricket
playing Foot Ball


abstract class Mobile { abstract int getPrice(); } class Redmi extends Mobile { int getPrice() { return 5000; } } class Samsung extends Mobile { int getPrice() { return 6000; } } class ExampleMobile { public static void main(String args[]) { Mobile m; m = new Redmi(); System.out.println("Price of Redmi mobile is: " + m.getPrice()); m = new Samsung(); System.out.println("Price of Samsung mobile is: " + m.getPrice()); } }

Output:

5000
6000

 

Read more topics related to java

 

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

19 Replies to “Abstract Class and Abstraction in Java.”

  1. I needed to put you the very little remark to be able to thank you the moment again on your pretty suggestions you have provided on this page. This has been simply strangely open-handed with you to offer unhampered precisely what many people would’ve offered for sale for an ebook to help make some money on their own, particularly seeing that you might have tried it in case you desired. Those strategies in addition worked to become good way to know that some people have the identical keenness really like my personal own to realize much more when considering this matter. I believe there are several more pleasant sessions up front for people who read through your site.

  2. I am commenting to let you know of the fabulous encounter my cousin’s daughter experienced reading your site. She even learned many issues, not to mention what it’s like to have a wonderful giving mindset to have many people just grasp chosen grueling subject areas. You really exceeded our expectations. Many thanks for distributing the informative, healthy, educational and as well as easy thoughts on the topic to Jane.

  3. I am only writing to make you understand what a remarkable experience our princess undergone viewing your webblog. She picked up some things, which included what it is like to possess an ideal teaching spirit to make the mediocre ones without difficulty master specified tortuous things. You undoubtedly surpassed readers’ expected results. Thanks for rendering those useful, trustworthy, revealing as well as unique tips on your topic to Mary.

  4. I am commenting to let you know of the fantastic encounter my cousin’s daughter experienced reading your site. She even learned many issues, not to mention what it’s like to have a wonderful giving mindset to have many people quite simply grasp chosen grueling subject areas. You really exceeded our expectations. Many thanks for giving the informative, healthy, educational and as well as easy thoughts on the topic to Jane.

  5. I simply needed to thank you so much once again. I am not sure the things that I would’ve implemented without the type of secrets provided by you on such a question. It previously was a very frustrating issue in my opinion, however , observing the very specialised strategy you processed that made me to leap over gladness. I am just happy for this information and in addition hope that you know what a great job you are always getting into teaching people thru your web page. More than likely you have never got to know any of us.

  6. I am just writing to make you understand what a notable experience my wife’s girl had using your web site. She mastered so many pieces, which included what it is like to possess an excellent helping nature to let other people with ease learn some problematic things. You truly surpassed people’s expected results. Thank you for producing these precious, trusted, informative and in addition fun tips about this topic to Lizeth.

  7. I am only writing to make you understand what a remarkable experience our princess undergone viewing your webblog. She picked up some things, which included what it is like to possess an ideal teaching spirit to make the mediocre ones without difficulty master specified tortuous things. You undoubtedly surpassed readers’ expected results. Thanks for rendering those useful, trustworthy, revealing as well as unique tips on your topic to Mary.

  8. I simply wanted to thank you very much once more. I do not know what I would have tried without these tactics shared by you regarding that subject matter. It was actually an absolute scary problem in my position, nevertheless taking note of this specialized tactic you solved the issue took me to weep with happiness. Now i’m thankful for your support and thus sincerely hope you really know what an amazing job you have been putting in training the mediocre ones via your webblog. Most probably you haven’t met any of us.

  9. I am just writing to make you understand what a perfect experience my wife’s girl obtained using your webblog. She noticed so many pieces, which included what it is like to possess an excellent helping spirit to let others with no trouble learn about specific specialized things. You truly surpassed readers’ expected results. Thank you for providing these priceless, trusted, informative as well as fun tips about this topic to Lizeth.

  10. I simply needed to thank you so much once again. I am not sure the things that I would’ve implemented without the type of secrets provided by you on such a question. It seemed to be a very frustrating issue in my opinion, however , observing the very specialised strategy you processed that made me to leap over gladness. I am just happy for this information and in addition pray you know what a great job you happen to be getting into teaching people thru your web page. More than likely you have never got to know any of us.

  11. I happen to be writing to make you understand what a wonderful experience our princess went through visiting yuor web blog. She realized too many things, with the inclusion of what it is like to possess an incredible teaching style to make the rest without problems understand various very confusing topics. You undoubtedly surpassed visitors’ expected results. Thanks for supplying those warm and helpful, trustworthy, revealing not to mention unique tips on your topic to Tanya.

  12. My wife and i have been so peaceful when Michael managed to finish up his researching via the precious recommendations he obtained using your web site. It is now and again perplexing to simply happen to be making a gift of tactics which often some others might have been trying to sell. We recognize we need you to thank for this. The type of illustrations you made, the straightforward website navigation, the relationships your site make it easier to instill – it is most sensational, and it is letting our son in addition to us reckon that this subject matter is interesting, which is certainly tremendously serious. Thanks for the whole lot!

  13. My wife and i have been really lucky when Louis managed to finish off his preliminary research through the entire precious recommendations he grabbed out of your web pages. It is now and again perplexing to just continually be giving out procedures which others may have been selling. So we grasp we have got the writer to give thanks to for that. The main illustrations you have made, the simple web site navigation, the relationships you will help foster – it’s got many impressive, and it’s really helping our son in addition to the family know that the matter is excellent, which is quite mandatory. Thank you for everything!

  14. Thanks for the advice on credit repair on this excellent site. Some tips i would tell people should be to give up this mentality that they may buy currently and fork out later. Being a society many of us tend to make this happen for many issues. This includes vacation trips, furniture, along with items we’d like. However, you’ll want to separate your current wants out of the needs. If you are working to boost your credit score actually you need some trade-offs. For example you’ll be able to shop online to save cash or you can look at second hand merchants instead of high-priced department stores pertaining to clothing.

  15. I precisely desired to say thanks again. I’m not certain the things I could possibly have carried out in the absence of the creative concepts discussed by you concerning my area of interest. Entirely was a daunting concern for me, but coming across a expert avenue you handled it forced me to cry for contentment. I’m grateful for the assistance and as well , expect you comprehend what a powerful job that you are carrying out educating many people by way of a web site. I am certain you’ve never come across all of us.

Leave a Reply

Your email address will not be published.