Skip to main content

Abstract classes & Interfaces

Abstract -Abstract in English means existing in thought or as an idea without concrete existance.

Abstract method-A method that is declared without an implementation

abstract void moveTo(double x ,double y)

Abstract class-If a class includes abstract methods, then the class itself must be declared abstract, as in:

public abstract class PhoneModel{

            abstract void switchoff();

                    //code}

When an abstract class is subclass the subclass usually provides implementation for all of the methods in parent class If it doesn't , It must be declared abstract be declared abstract.

Note-It is possible to create reference of or abstract class. 

         It is not possible to create an object of an abstract class.

We can also assign reference of an abstract class to the object of a concrete subclass.

Interfaces in Java-Interface in English is a point where two systems meet and intrace.

In Java interface is a group of related methods with empty bodies.

class AvonCycle implements Bycycle{

Example-

interface Bicycle{

void applyBrake(int decrement);

void speedUp(int increment);

}

int speed=7;

void applyBrake(int decrement){

speed=speed-decrement;

}

}

Abstract class vs Interfaces

We cant extend multiple abstract classes but we can implement multiple interfaces at a time. Interfaces are meant for dynamiic method dispatch and run time polymorphism.

Is multiple inheritance allowed in java?

Multiple inheritance face problems when there exist methods with some signature in both the super classes.

Due to such problems, Java does not support multiple inheritance directly but the similar  concept can be achieved using interfaces.

A class can implement multiple interfaces and extend a class at the same time.





Comments

Popular posts from this blog

How to create a new project in android studio

 step1-Open your android studio in you computer. steip2-Go to file in your title bar. step3-Open new and select new project. After it there are many basic project templates which we have to select  since we are preparing here a basic app so we would like select empty project template. step4-Click on next A  new dialog box appear in which you have to fill the name of your own project. After filling it Package name of your project will be filled by itself. you can also customize you project location where you want. step5-select your language . step6-Select you Minimum SDK(which will sure you how much android system your app will run. for a good experience- API 16 : Android 4.1(jelly Bean) suggested. step6-Click on finish  After indexing Your new project will be set.

Add video in web page

Nodejs tutorial 1