assignment 4 # Abstract and interface - mehnazchaudary9595/java-learning GitHub Wiki
what is abstract class in java?
a class in which contains abstract keyword called abstract class. it can have two method .
abstract method
non abstract method.
Abstraction in Java
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 for example sending
sms, you just type the text and send the message. You don't know the internal processing about the message delivery.
Abstract methods:
A method that is declared as abstract and does not have implementation is known as abstract method .
its needs to be extend ed and its method implemented. abstract class never static and final. it is a process of hiding the implementaion details and only show functionaly.
Any class that extends an abstract class must implement all the abstract methods declared by the super class.
Abstract class inherit from other class and provide implementations abstract methods in it.
syntax
Abstract class class_name{ }
Example:
abstract class animal{
abstract void run();
}
class dog extends animal
{
void run()
{
System.out.println(" dog run very fast");
}
public static void main(String args[]){
dog obj = new animal();
obj.run();
}
}
what is INTERFACE IN JAVA:
an interface is a referance type,similar to a class ,that can contain only constants ,method signatures, ststic methods and nested types.interface look like a class but it is not a class.
Also, the variables declared in an interface are public, static & final by default. They can only be implemented by classes or extended by other interfaces.
Example:
interface Print Table{
void Print Table();
}
class Table implements Print Table{
public void Print Table(){
int x=5;
for(int i=1;i<10;i++)
{
System.out.println(x+" * " + i + " = "+x * i);
}
public static void main(String args[]){
Table obj = new Table();
obj.Print Table();
}
}
output
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50