Abstract class:

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

Why should we use Abstract class:

Java Abstract class is used to provide common method implementation to all the subclasses or to provide default implementation.

  • Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
  • Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

Program:

abstract class Village{
String Villagename=”theni”;
int noofppl=24;
void green(){
System.out.println(“green”);
}
void agriculture(){
System.out.println(“agriculture”);
}
/*public Village(String V,int np){
V=Villagename;
np=noofppl;
System.out.println(“Village”);

}*/
}

subclass:

class Cultivation extends Village{
int noofseed;
String seedname;
public static void main(String args[]){
Cultivation C=new Cultivation();
C.green();
C.agriculture();
}
}

output:

sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Cultivation.java
sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Cultivation
green
agriculture


Leave a comment

Design a site like this with WordPress.com
Get started