Solved

abstract class with all non abstract mentods

Posted on 2016-08-13
6
71 Views
Last Modified: 2016-08-14
Hi,
Can a abstract class with all non abstract mentods? Is that is possible. I am under impression that atleast one method of abstract class should be abstract. What are advantages and practical uses of abstract methods compared to interface. I see many cases abstract class is implements a interface. Then a concrete class extends the abstract class. I wonder what is use of this pattern

interface--->abstract class(implements)--->concrete class(extends)
0
Comment
Question by:gudii9
  • 2
  • 2
  • 2
6 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 250 total points
ID: 41755192
>> Can a abstract class with all non abstract methods?

Yes there's nothing wrong with that.  It's not common but it's allowed.

>> What are advantages and practical uses of abstract methods compared to interface.

Interfaces specify a list of methods with no implementations.
Abstract classes can include implementations (code) and fields (values).  So you can do more with an abstract class.

Why then not just use abstract classes everywhere?  Because a given concrete class can only inherit from at most 1 abstract class.  But it can implement as many interfaces as it wants.

So interfaces are more flexible.

Doug
0
 
LVL 10

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 250 total points
ID: 41755205
Doug is correct except that in the newest version of Java they have introduced the ability to but a default implementation of a method in an interface. This gives a little more flexibility in trying to do multi inheritance in a class.

Usually the benefit of interfaces is it really helps you if you are going to have multiple classes that need to have a common set of methods but don't entirely work the same.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41755544
Doug is correct except that in the newest version of Java they have introduced the ability to but a default implementation of a method in an interface. This gives a little more flexibility in trying to do multi inheritance in a class.

any good links or examples on this?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 26

Expert Comment

by:dpearson
ID: 41755591
Yeah I didn't mention default implementations as I'm concerned it may confuse your understanding if you're not clear on abstract classes vs interfaces yet.

But anywhere here's some info on them:
https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

(Oracle added this capability so they could extend some existing interfaces in the Java language without breaking a ton of existing code).

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 41755873
>> Can a abstract class with all non abstract methods?

Yes there's nothing wrong with that.  It's not common but it's allowed.

it is kind of defeating the purpose for which we create abstract class right?
if we do not have abstract methods in it it seems very uncommon to me.
0
 
LVL 10

Expert Comment

by:Jeffrey Dake
ID: 41755898
Not always, you may just have a whole bunches of functions you want inherited but don't want that base class to ever be instantioated.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now