+ 4

What is the difference between interface and abstract classes?

25th Jun 2017, 9:52 AM
MANOJ KUMAR
MANOJ KUMAR - avatar
3 ответов
+ 7
Interface: Interface are just like a Contract what a class gets into & the class should follow the contract. Incomplete methods inherited from an Interface should be Completed in a Class. If you don't Complete the method in the class then you will get an Error. Difference in Example Wise : Public interface A { public void test (); //An Incomplete Method } Public class B implements A { public static void main(String[] args) { B b= new B(); b.test(); } } Key Difference for you : - Interface can story only incomplete methods - By default every method is abstract in an Interface. - Every variable by default in an Interface is Public Static & Final. - Multiple Inheritance is Supported in Interface's. - We can Never create the Object of Interface. - We can Not keep a Main Method in an Interface. Abstract Class: Abstract classes are Partial incomplete in there Nature. Every method should have abstract keyword to specify that it is Incomplete. Difference in Example Wise : public abstract class A { public abstract void test1(); //An Incomplete Method public void test2(); { System.out.println("Inside A"); } } Public class B implements A { public void test1() { System.out.println("Inside A"); } public static void main(String[] args) { B b= new B(); b.test1(); b.test2(); } } Key Difference for you : - An abstract class can consist of both Complete & Incomplete methods in it. - Every method by default is Non-Abstract. - We should use abstract keyword to specificy in complete method. - Variable by default is Not Final. - Multiple Inheritance in Abstract class is not possible. - Inheritance between an Abstract class & Interface is possible.
27th Jul 2017, 1:36 AM
Gings
Gings - avatar
+ 2
how come there are so many questions about interfaces and abstract classes? https://www.sololearn.com/Discuss/363907/?ref=app
25th Jun 2017, 1:27 PM
Thanh Le
Thanh Le - avatar
+ 1
As far as I know from C# and Java: Abstract Class: - needs to be inherited - can contain state members and implementations of methods - derived classes don't require to implement the abstract methods Interfaces: - needs to be implemented - doesn't have any form of implementation - classes that implement interfaces require to implement every method of each interface used You can only use one abstract class because of the single-inheritance limitations of C# and Java, but you can implement multiple interfaces. Feel free to add/correct me if I missed something.
25th Jun 2017, 11:12 AM
Kagemi