answersLogoWhite
C Programming
C Sharp
ASP.NET

What is abstract and interface?


Top Answer
User Avatar
Wiki User
Answered 2014-03-08 14:06:09

abstract is keyword this is used for define abstract methods and abstract classes.abstract method means if you want define a method with out any body at that time you should define a method with abstract keyword.

EX:public abstract int add()

{

}

if you want define a class with abstract method must and should you define a class will be abstract class.

Abstract EX:abstract class abc

{

abstract methods

------------

non abstract methods

----------------

}

you can define abstract as well as non abstract methods in abstract class.

in normal class you can define only normal methods only.

you can't acess the methods which are in the abstract class from the same clss.by child class only you can acess the methods which are in abstract class.

if you want to inherit abstract class must and should you have to define abstract methods which are in abstract class.

Interface

----------:-

interface is also like a clss.in interface you have to define only abstract methods only.you can't define normal methods in interface.

EX: interface abc

{

abstract methods

}

by inharitance only you can acess the methods what are in the interface through the class.

001
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
๐Ÿ˜‚
0
User Avatar

Your Answer

Related Questions


All the methods declared inside an Interface are abstract. Where as abstract class must have at least one abstract method and others may be concrete or abstract. In Interface we need not use the keyword abstract for the methods.


Comparison between an Abstract Class and an Interface:While an abstract class can define both abstract and non-abstract methods, an interface can have only abstract methods. Another way interfaces differ from abstract classes is that interfaces have very little flexibility in how the methods and variables defined in the interface are declared. These rules are strict:


An abstract interface is an interface which has a one-to-many relation - providing multiple paths from a single location to multiple locations.


If a class has one abstract method ,the class has to be an abstract class.Methods can be implemented in abstract class.Whereas a interface is like a abstract class...the only difference being that the methods are never implemented in Interface.


The difference are:An abstract class can have methods that actually have code inside them whereas an Interface cannot (An Interface can be thought of as a 100% pure abstract class)All variables in an Interface are public, static, final whereas that is not the case in Abstract classes


Some difference between an interface and an abstract class are: a. All variables in an interface are public, static and final but that is not the case in abstract classes b. An abstract class can have both abstract and concrete methods but an interface cannot have concrete methods c. An abstract class can extend other classes and implement interfaces, while an interface can only extend other interfaces.


Comparison between an Abstract Class and an Interface:While an abstract class can define both abstract and non-abstract methods, an interface can have only abstract methods. Another way interfaces differ from abstract classes is that interfaces have very little flexibility in how the methods and variables defined in the interface are declared. These rules are strict:• All interface methods are implicitly public and abstract. In other words, you do not need to actually type the public or abstract modifiers in the method declaration, but the method is still always public and abstract. (You can use any kind of modifiers in the Abstract class)• All variables defined in an interface must be public, static, and final-in other words, interfaces can declare only constants, not instance variables.• Interface methods must not be static.• Because interface methods are abstract, they cannot be marked final, strictfp, or native. (More on these modifiers later.)• An interface can extend one or more other interfaces.• An interface cannot extend anything but another interface.• An interface cannot implement another interface or class.• An interface must be declared with the keyword interface.You must remember that all interface methods are public and abstract regardless of what you see in the interface definition.


All interfaces are abstract.


While an abstract class can define both abstract and non-abstract methods, an interface can have only abstract methods. Another way interfaces differ from abstract classes is that interfaces have very little flexibility in how the methods and variables defined in the interface are declared. These rules are strict: • All interface methods are implicitly public and abstract. In other words, you do not need to actually type the public or abstract modifiers in the method declaration, but the method is still always public and abstract. (You can use any kind of modifiers in the Abstract class) • All variables defined in an interface must be public, static, and final-in other words, interfaces can declare only constants, not instance variables. • Interface methods must not be static. • Because interface methods are abstract, they cannot be marked final, strictfp, or native. (More on these modifiers later.) • An interface can extend one or more other interfaces. • An interface cannot extend anything but another interface. • An interface cannot implement another interface or class. • An interface must be declared with the keyword interface. You must remember that all interface methods are public and abstract regardless of what you see in the interface definition.


An abstract class is an interface. Ideally, an abstract class should be nothing but interface (no non-static data members).


Abstract class is to define "IS_A..." Interface is to define "Behave like ..." (not necessary to be a ...") An abstract class if with all public abstract methods may look just like an interface. An abstract class may also provide some implementation of those APIs (public methods), while an interface is to decouple (separate) the intend from the implementation


My answer to "Can an interface without any abstract method?" is YES.If abstract methods do not include the properties, then this interface should have at least 1 property (get or set) defined. It makes sense to have at least one of the abstract method or property/mutator/getter-setter.If the interface contains no properties, no methods, I cannot think of a reason to create such an interface, meaning - it is useless.


Differences:Abstract class can also contain method definitions but an interface can contain only declarationsAll variables in an interface are by default public static and final whereas in Abstract class it is notAn interface can be considered as a pure abstract class that contains no method implementations and contains only declarations.


Interface is collection of abstract methods which has only declaration and no implementation


An interface is like a 100% abstract class. Only abstract methods can go in an interface. In addition, all methods in an abstract class are public and abstract by default. You "extend" an interface using the keyword implements. You can implement multiple interfaces, but you have to extend only one class. An interface can have only constants. This means no instance variables.As for an abstract class, you can put both abstract and concrete methods in an abstract class. It can have instance variables, but you can only extend one abstract class.Sun recommends using abstract and concrete class extension to signify what a class is, with an example in the following inheritance tree:Object||-Person||-Programmer||-JavaProgrammerAn interface is for signifying what jobs a class can do, like a Message class implementing a Sendable interface. Also, class names should be nouns and interface names should be adjectives, but conventions vary widely among programmers.


Some differences are:An abstract class can have code for one or more methods but an interface cannotAll variables in an interface are public static and final but in an abstract class it is notAbstract classes are faster than interfaces


abstract is when you scribble lines and whenever you are done with scribbling the scribbles end up looking like a body or face and interface is when you draw all of the lines, joints, and bones in the human face. (trust me I went to class for abstract and interface!)


We can't instantiate both interfaces and abstract classes.The only one difference between them is that an interface can't contain concrete(fully defined) methods where as an abstract class may contain them.An abstract class not necessarily contain abstract methods. we can make a class as abstract class even it does not has any abstract methods.When there is a need to write both abstract and concrete methods in a single unit we have to use an abstract class instead of an interface since an interface cant contain concrete methods.All the fields(or properties) of an interface are by default 'static final' even when you don't mention explicitly. And all methods are 'public abstract'.But in an abstract class we can have any type of fields and methods.


include all abstract method


No, not directly. You cannot use new operator to create either. However, you may create an instance of derived class from the abstract class, and treat (by casting, for example) as an instance of the abstract class. Similar to an interface: create an object from a class that implements the target interface, then treat that object as an instance of that interface.


An interface in Java is like an abstract class, but there are no method bodies allowed in it and it has to be declared with the interface keyword. It is Java's way of getting around the Deadly Diamond of Death. Only abstract methods and constants are allowed in it.


An abstract class has at least abstract methods. An interface has only abstract actions. For example, the javax.swing.AbstractAction class has only one abstract method that must be overriden: public void actionPerformed(ActionEvent e). The java.awt.event.ActionListener interface has the same method that must be overriden; however, it is its only method. The AbstractAction class is a subclass of the Action class, which implements the ActionListener interface. The AbstractAction class has overriden all but one of the Action class's methods.


In general, the differences are that interface has(1) no fields and(2) no implementation of methodsbut in UML interface may have features (fields), so the difference left is that interface in UML has no implemented methods while abstract class by definition is partially implemented class.


The idea is that an interface is not a class, it is just a specification of what classes that implement it must contain.


The abstract keyword signifies that the particular method will have no features in the class where it is declared and it is upto the child class to provide the functionality. In case of an interface, the method is already abstract by default and has no code inside it. So there is no actual point in using the abstract keyword there.



Copyright ยฉ 2021 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.