Skip to main content
What is the difference between Abstract Class and Interface?
Abstract classes usually represent an abstract concept or an entity with partial or no implementation. On the other hand, an interface is an abstract type that is used to specify a contract that should be implemented by classes. Abstract classes should be inherited (or extended), while interfaces should be implemented. Abstract classes may contain abstract methods, whereas an interface should only contain abstract methods. Abstract classes can contain any variables, but Interfaces can only define constants. A class cannot inherit from more than one abstract class but can implement multiple interfaces. An Interface cannot implement another interface. However an interface can extend a class.
// I say all motor vehicles should look like this:
interface MotorVehicle
{
    void run();

    int getFuel();
}

// my team mate complies and writes vehicle looking that way
class Car implements MotorVehicle
{

    int fuel;

    void run()
    {
        print("Wrroooooooom");
    }


    int getFuel()
    {
        return this.fuel;
    }
}

// I say all motor vehicles should look like this :
abstract class MotorVehicle
{

    int fuel;

    // they ALL have fuel, so why not let others implement this?
    // let's make it for everybody
    int getFuel()
    {
         return this.fuel;
    }

    // that can be very different, force them to provide their
    // implementation
    abstract void run();


}

// my team mate complies and writes vehicle looking that way
class Car extends MotorVehicle
{
    void run()
    {
        print("Wrroooooooom");
    }
}

Comments

Popular posts from this blog

ifference between @RestController and @Controller

Difference between @RestController and @Controller Annotation in Spring MVC and REST Read more:  http://javarevisited.blogspot.com/2017/08/difference-between-restcontroller-and-controller-annotations-spring-mvc-rest.html#ixzz4sbGy2Glh The  @RestController  annotation in Spring MVC is nothing but a combination of  @Controller  and  @ResponseBody  annotation. It was added into Spring 4.0 to make the development of RESTful Web Services in Spring framework easier. If you are familiar with the  REST web services  you know that the fundamental difference between a web application and a REST API is that the response from a web application is generally view (HTML + CSS + JavaScript) while REST API just return data in form of JSON or XML. This difference is also obvious in the  @Controller  and  @RestController  annotation. The job of  @Controller  is to create a Map of model object and find a view but  @RestContr...

Presentation topics

Topics for presentation : Knockout.js, Angular.js, Node,js, Kendo UI, MVC 4, Nuget, Ninject, Unity Framework, Dependency Injection , PMC Package Manager Control, Mordenizer, Moq, Json, Html5, Entity Framework code1st. Spring.net Castle Windsor , Structure Map and Microsoft Unity ,
What happens when you compile/run the following code: class MyClass { public static void main(String[] args) { new MyClass(); } Ans: It executes and create a object for that class without reference. The output of the program is nothing.