Abstract Classes and Abstract Methods
abstractis a keyword.abstractkeyword can be used in two ways:- with classes
- with methods
- Classes defined without abstract modifier are called as concrete class. Concrete class can be instantiated.
- Classes defined with abstract modifier are called as abstract class. Abstract class cannot be instantiated but can be subclassed.
- Methods defined without abstract modifier are called as concrete method. Concrete method must have body.
- Methods defined with abstract modifier are called as abstract method. Abstract method must not have body.
- Abstract class can be used when you want to define default behavior for subclasses.
Lab529.java
classShape{
voidarea(){ }
}
classSquareextendsShape{ }
classLab529{
publicstaticvoidmain(Stringargs[]){
Shapesp=newSquare();
sp.area();
}
}
Lab530.java
classShape{
voidarea();// Error
}
classSquareextendsShape{ }
classLab530{
publicstaticvoidmain(Stringargs[]){
Shapesp=newSquare();
sp.area();
}
}
Lab531.java
classShape{
abstractvoidarea();
}
classSquareextendsShape{ }
classLab531{
publicstaticvoidmain(Stringargs[]){
Shapesp=newSquare();
sp.area();
}
}
Lab532.java
abstractclassShape{
abstractvoidarea();
}
classLab532{
publicstaticvoidmain(Stringargs[]){
Shapesp=newShape();
sp.area();
}
}
Lab533.java
abstractclassShape{
abstractvoidarea();
}
classSquareextendsShape{ }
classLab533{
publicstaticvoidmain(Stringargs[]){
Shapesp=newSquare();
sp.area();
}
}
Lab534.java
abstractclassShape{
abstractvoidarea();
}
classSquareextendsShape{
voidarea(){
System.out.println("Square -> area()");
}
}
classLab534{
publicstaticvoidmain(Stringargs[]){
Shapesp=newSquare();
sp.area();
}
}
Lab535.java
abstractclassPerson{
abstractvoidsleeping();
abstractvoidwalking();
}
abstractclassStudentextendsPerson{
voidsleeping(){
System.out.println("Student -> sleeping()");
}
}
classCurrentStudentextendsStudent{
voidwalking(){
System.out.println("CurrentStudent -> walking()");
}
}
classLab535{
publicstaticvoidmain(Stringargs[]){
Personpob=newCurrentStudent();
pob.sleeping();
pob.walking();
}
}
Lab536.java
abstractclassPerson{
abstractvoidsleeping();
}
abstractclassStudentextendsPerson{
voidsleeping(){
System.out.println("Student -> sleeping()");
}
}
classCurrentStudentextendsStudent{ }
classLab536{
publicstaticvoidmain(Stringargs[]){
Personpob=newCurrentStudent();
pob.sleeping();
}
}
Lab537.java
abstractclassHello{
staticintA=1234;
staticvoidshow(){
System.out.println("Hello -> show()");
}
}
classLab537{
publicstaticvoidmain(String[]args){
System.out.println(Hello.A);
Hello.show();
}
}
Lab538.java
abstractclassHello{
inta;
Hello(inta){
this.a=a;
System.out.println("Hello (int) Cons");
}
{
System.out.println("Hello -> I.B");
}
voidshow(){
System.out.println("Hello -> show()");
}
}
classHaiextendsHello{
Hai(inta){
super(a);
}
}
classLab538{
publicstaticvoidmain(String[]args){
Haihai=newHai(10);
hai.show();
}
}
Lab539.java
abstractclassLab539{
publicstaticvoidmain(String[]args){
System.out.println("Lab539 Main Method");
}
}