Abstraction in Java, Java Object-Oriented Programming, Abstract Class, Abstract Methods, Java Interfaces, and Inheriting Abstract classes.
Java Abstraction
Four principles of Java object-oriented program,
1. Inheritance
2. Polymorphism
3. Abstraction
4. Encapsulation
Java Object-Oriented Programming – Abstraction
Abstraction is a Process of hiding implementation details and showing only functionality to the user.
Two types of Methods
1. Concrete Methods (The Methods which are having body)
Syntax:
accessModifier retrunType / returnTypeNothing method Name() {
Statements
———-
———–
————-
}
2. Abstract Methods (The Methods which are not having bodies)
Syntax:
accessModifier abstract returnType / returnTypeNothing MethodName();
• If we know the method name, but don’t know the method functionality then we go for Abstract Methods
• Java Class contains 100% concrete Methods.
• Abstract Class contains one or more abstract methods.
Example:
Class1 (having 10 methods)
10 Methods are concrete Methods.
It is a Java Class
——————————
Class2 (having 10 methods)
5 Methods are concrete Methods and 5 methods are abstract methods)
It is an Abstract Class
——————————–
Class3 (having 10 Methods)
All Methods are Abstract Methods
It is an Abstract Class
Example for Abstract Class:
public abstract class Bikes {
public void handle(){
System.out.println(“Bikes have Handle”);
}public void seat(){
System.out.println(“Bikes have Seats”);
}public abstract void engine();
public abstract void wheels();
public static void main(String[] args) {
//Bikes obj = new Bikes();}
}
Note: We cannot create Object/Instance in Abstract Class.
Note 2: In order to use Methods from Abstract Class then first we need to implement abstract methods in sub-class.
Use methods from Abstract Class:
public class HeroHonda extends Bikes{
public void engine() {
System.out.println(“Bikes have Engine”);
}public void wheels() {
System.out.println(“Bikes have Wheels”);
}public static void main (String [] args){
HeroHonda abc = new HeroHonda();abc.seat();
abc.engine();
abc.wheels();
abc.handle();
}
}
Java Interfaces
• Interface is a Java-type definition block that is 100% abstract.
1. Class vs. Abstract Class
Java Classes have 100% concrete Methods
Abstract Classes have one or more/all abstract methods.
2. Class vs. Interface
Java Classes have 100% concrete Methods
Java Interfaces have 100% Abstract Methods
3 Abstract Class vs. Interface
Abstract Classes have one or more/all abstract methods.
Java Interfaces have 100% Abstract Methods
• All Interface Methods by default public and abstract.
• static and final modifiers are not allowed for Interface methods
• In Interfaces Variables have to initialize at the time of declaration only
Ex:
int a;//Incorrect
a=100;
int b=200;//Correct
• In Interfaces Variables are public and final by default.
• Interfaces are going to be used using the “implements” keyword.
Create Java Interface
Java Project
Java Package
Java Class / Java Interface
Java Interface Example:
public interface Interface1 {
public void engine();
public void seat();
public void wheels();
public void handle();
}
Reuse Methods from Interface to Class
public class ClassNew implements Interface1 {
public void engine() {
System.out.println(“Bikes have Engine”);
}public void seat() {
System.out.println(“Bikes have Seat”);
}public void wheels() {
System.out.println(“Bikes have Wheels”);
}
public void handle() {
System.out.println(“Bikes have Handle”);
}
public static void main (String [] args){
ClassNew obj = new ClassNew();
obj.seat();
obj.wheels();
obj.handle();
}
}
Note:
• Reuse Methods from a Class to another Class – using “extends” keyword.
• Reuse Methods from an Abstract Class to Class – using the “extends” keyword.
• Reuse Methods from an Interface to Class – using the “implements” keyword.
Software Testing Tutorials