Abstraction in Java
Abstraction(Hide certain details and show only essential details):- Abstraction is a process of hiding the implementation details and showing only functionality to the user.Another way, it shows only important things to the user and hides the internal details.
How to achieve Abstraction in java
There are two ways to achieve abstraction in java
1. Abstract class (0 to 100%)
2. Interface (100%)
1.Abstract class:-
A class that is declared as abstract is known as abstract class.It needs to be extended and its method
implemented. It cannot be instantiated.
An abstract class is something which is incomplete and you can not create instance of abstract class. Java has concept of abstract classes, abstract method but a variable can not be abstract in Java.
Example of abstract class
abstract class Shape{
abstract void draw(); //here we hide implementation
}
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle extends Shape{
void draw(){System.out.println("drawing circle");}
}
class Test{
public static void main(String args[]){
Shape s=new Circle();
s.draw();
}
}
------------------------------------------------------------------------------------------------------------
*1.If there is any abstract method in a class, that class must be abstract.
*2.An abstract method in Java doesn't have body , its just a declaration. In order to use abstract method you need to override that method in SubClass.
------------------------------------------------------------------------------------------------------------
2. Interface:-An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements.
An interface is similar to a class in the following ways:
1.An interface can contain any number of methods.
2.An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
3.The bytecode of an interface appears in a .class file.
4.Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.
However, an interface is different from a class in several ways, including:
1.You cannot instantiate an interface.
2.An interface does not contain any constructors.
3.All of the methods in an interface are abstract.
4.An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
5.An interface is not extended by a class; it is implemented by a class.
6.An interface can extend multiple interfaces.
Example of interface
interface Vehicle {
void showMaxSpeed();//must be implemented in all classes which implement this interface
}
class Bus implements Vehicle {
public void showMaxSpeed() {
System.out.println("Bus max speed: 100 km/h");
}
}
class Truck implements Vehicle {
public void showMaxSpeed() {
System.out.println("Truck max speed: 80 km/h");
}
}
public class InterfaceExample {
public static void main(String[] args) {
Vehicle bus = new Bus();
Vehicle truck = new Truck();
bus.showMaxSpeed();
truck.showMaxSpeed();
}
}
------------------------------------------------------------------------------------------------------------
Java Abstract class and interface Interview Questions
**Difference Between Interface and Abstract Class
(i)Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
(ii)Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
(iii)Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
(iv)Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
(v)An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
(vi)A Java class can implement multiple interfaces but it can extend only one abstract class.
(vii)Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
(viii)In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.
1.Can abstract class have constructors in Java?
Yes, abstract class can declare and define constructor in Java. Since you can not create instance of abstract class, constructor can only be called during constructor chaining, i.e. when you create instance of concrete implementation class. Now some interviewer, ask what is the purpose of constructor, if you can not instantiate abstract class? Well, it can still be used to initialize common variables, which are declared inside abstract class, and used by various implementation. Also even if you don’t provide any constructor, compiler will add default no argument constructor in abstract class, without that your subclass will not compile, since first statement in any constructor implicitly calls super(), default super class constructor in Java.
2.Can you create instance of abstract class?
No, you can not create instance of abstract class in Java, they are incomplete. Even though, if your abstract class don’t contain any abstract method, you can not create instance of it. By making a class abstract, you told compiler that, it’s incomplete and should not be instantiated. Java compiler will throw error, when a code tries to instantiate abstract class.
3.What is an abstract method?
An abstract method is a method whose implementation is deferred to a subclass.
4.Is it necessary for abstract class to have abstract method?
No, It’s not mandatory for an abstract class to have any abstract method. You can make a class abstract in Java, by just using abstract keyword in class declaration. Compiler will enforce all structural restriction, applied to abstract class, e.g. now allowing to create any instance. By the way, it’s debatable whether you should have abstract method inside abstract class or interface. In my opinion, abstract class should have abstract methods, because that’s the first thing programmer assumes, when he see that class. That would also go nicely along principle of least surprise.
5.Can abstract class implements interface in Java? does they require to implement all methods?
Yes, abstract class can implement interface by using implements keyword. Since they are abstract, they don’t need to implement all methods. It’s good practice to provide an abstract base class, along with an interface to declare Type. One example of this is java.util.List interface and corresponding java.util.AbstractList abstract class. Since AbstractList implements all common methods, concrete implementations like LinkedList and ArrayList are free from burden of implementing all methods, had they implemented List interface directly. It’s best of both world, you can get advantage of interface for declaring type, and flexibility of abstract class to implement common behavior at one place. Effective Java has a nice chapter on how to use interface and abstract class in Java, which is worth reading.
6.Can abstract class be final in Java?
No, abstract class can not be final in Java. Making them final will stop abstract class from being extended, which is the only way to use abstract class. They are also opposite of each other, abstract keyword enforces to extend a class, for using it, on the other hand, final keyword prevents a class from being extended. In real world also, abstract signifies incompleteness, while final is used to demonstrate completeness. Bottom line is, you can not make your class abstract and final in Java, at same time, it’s a compile time error.
7.Can abstract class have static methods in Java?
Yes, abstract class can declare and define static methods, nothing prevents from doing that. But, you must follow guidelines for making a method static in Java, as it’s not welcomed in a object oriented design, because static methods can not be overridden in Java. It’s very rare, you see static methods inside abstract class, but as I said, if you have very good reason of doing it, then nothing stops you.
8.Is it necessary for abstract class to have abstract method?
No, It’s not mandatory for an abstract class to have any abstract method. You can make a class abstract in Java, by just using abstract keyword in class declaration. Compiler will enforce all structural restriction, applied to abstract class, e.g. now allowing to create any instance.
9.What is abstract method in Java?
An abstract method is a method without body. You just declare method, without defining it and use abstract keyword in method declaration. All method declared inside Java Interface are by default abstract. Here is an example of abstract method in Java
public void abstract printVersion();
Now, In order to implement this method, you need to extend abstract class and override this method.
10.Can abstract class contains main method in Java ?
Yes, abstract class can contain main method, it just another static method and you can execute Abstract class with main method, until you don’t create any instance.
Abstraction(Hide certain details and show only essential details):- Abstraction is a process of hiding the implementation details and showing only functionality to the user.Another way, it shows only important things to the user and hides the internal details.
How to achieve Abstraction in java
There are two ways to achieve abstraction in java
1. Abstract class (0 to 100%)
2. Interface (100%)
1.Abstract class:-
A class that is declared as abstract is known as abstract class.It needs to be extended and its method
implemented. It cannot be instantiated.
An abstract class is something which is incomplete and you can not create instance of abstract class. Java has concept of abstract classes, abstract method but a variable can not be abstract in Java.
Example of abstract class
abstract class Shape{
abstract void draw(); //here we hide implementation
}
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle extends Shape{
void draw(){System.out.println("drawing circle");}
}
class Test{
public static void main(String args[]){
Shape s=new Circle();
s.draw();
}
}
------------------------------------------------------------------------------------------------------------
*1.If there is any abstract method in a class, that class must be abstract.
*2.An abstract method in Java doesn't have body , its just a declaration. In order to use abstract method you need to override that method in SubClass.
------------------------------------------------------------------------------------------------------------
2. Interface:-An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements.
An interface is similar to a class in the following ways:
1.An interface can contain any number of methods.
2.An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
3.The bytecode of an interface appears in a .class file.
4.Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.
However, an interface is different from a class in several ways, including:
1.You cannot instantiate an interface.
2.An interface does not contain any constructors.
3.All of the methods in an interface are abstract.
4.An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
5.An interface is not extended by a class; it is implemented by a class.
6.An interface can extend multiple interfaces.
Example of interface
interface Vehicle {
void showMaxSpeed();//must be implemented in all classes which implement this interface
}
class Bus implements Vehicle {
public void showMaxSpeed() {
System.out.println("Bus max speed: 100 km/h");
}
}
class Truck implements Vehicle {
public void showMaxSpeed() {
System.out.println("Truck max speed: 80 km/h");
}
}
public class InterfaceExample {
public static void main(String[] args) {
Vehicle bus = new Bus();
Vehicle truck = new Truck();
bus.showMaxSpeed();
truck.showMaxSpeed();
}
}
------------------------------------------------------------------------------------------------------------
Java Abstract class and interface Interview Questions
**Difference Between Interface and Abstract Class
(i)Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
(ii)Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
(iii)Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
(iv)Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
(v)An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
(vi)A Java class can implement multiple interfaces but it can extend only one abstract class.
(vii)Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
(viii)In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.
1.Can abstract class have constructors in Java?
Yes, abstract class can declare and define constructor in Java. Since you can not create instance of abstract class, constructor can only be called during constructor chaining, i.e. when you create instance of concrete implementation class. Now some interviewer, ask what is the purpose of constructor, if you can not instantiate abstract class? Well, it can still be used to initialize common variables, which are declared inside abstract class, and used by various implementation. Also even if you don’t provide any constructor, compiler will add default no argument constructor in abstract class, without that your subclass will not compile, since first statement in any constructor implicitly calls super(), default super class constructor in Java.
2.Can you create instance of abstract class?
No, you can not create instance of abstract class in Java, they are incomplete. Even though, if your abstract class don’t contain any abstract method, you can not create instance of it. By making a class abstract, you told compiler that, it’s incomplete and should not be instantiated. Java compiler will throw error, when a code tries to instantiate abstract class.
3.What is an abstract method?
An abstract method is a method whose implementation is deferred to a subclass.
4.Is it necessary for abstract class to have abstract method?
No, It’s not mandatory for an abstract class to have any abstract method. You can make a class abstract in Java, by just using abstract keyword in class declaration. Compiler will enforce all structural restriction, applied to abstract class, e.g. now allowing to create any instance. By the way, it’s debatable whether you should have abstract method inside abstract class or interface. In my opinion, abstract class should have abstract methods, because that’s the first thing programmer assumes, when he see that class. That would also go nicely along principle of least surprise.
5.Can abstract class implements interface in Java? does they require to implement all methods?
Yes, abstract class can implement interface by using implements keyword. Since they are abstract, they don’t need to implement all methods. It’s good practice to provide an abstract base class, along with an interface to declare Type. One example of this is java.util.List interface and corresponding java.util.AbstractList abstract class. Since AbstractList implements all common methods, concrete implementations like LinkedList and ArrayList are free from burden of implementing all methods, had they implemented List interface directly. It’s best of both world, you can get advantage of interface for declaring type, and flexibility of abstract class to implement common behavior at one place. Effective Java has a nice chapter on how to use interface and abstract class in Java, which is worth reading.
6.Can abstract class be final in Java?
No, abstract class can not be final in Java. Making them final will stop abstract class from being extended, which is the only way to use abstract class. They are also opposite of each other, abstract keyword enforces to extend a class, for using it, on the other hand, final keyword prevents a class from being extended. In real world also, abstract signifies incompleteness, while final is used to demonstrate completeness. Bottom line is, you can not make your class abstract and final in Java, at same time, it’s a compile time error.
7.Can abstract class have static methods in Java?
Yes, abstract class can declare and define static methods, nothing prevents from doing that. But, you must follow guidelines for making a method static in Java, as it’s not welcomed in a object oriented design, because static methods can not be overridden in Java. It’s very rare, you see static methods inside abstract class, but as I said, if you have very good reason of doing it, then nothing stops you.
8.Is it necessary for abstract class to have abstract method?
No, It’s not mandatory for an abstract class to have any abstract method. You can make a class abstract in Java, by just using abstract keyword in class declaration. Compiler will enforce all structural restriction, applied to abstract class, e.g. now allowing to create any instance.
9.What is abstract method in Java?
An abstract method is a method without body. You just declare method, without defining it and use abstract keyword in method declaration. All method declared inside Java Interface are by default abstract. Here is an example of abstract method in Java
public void abstract printVersion();
Now, In order to implement this method, you need to extend abstract class and override this method.
10.Can abstract class contains main method in Java ?
Yes, abstract class can contain main method, it just another static method and you can execute Abstract class with main method, until you don’t create any instance.
No comments:
Post a Comment