Answers for "purpose of abstract class in java"

1

implementing an abstract class jaav

interface human {
    void dothings();
    void eatthings();
    void sleep();
}
abstract class student implements human {
    public void dothings()
    {
        System.out.println("doing something");
    }
    public void eatthings()
    {
        System.out.println("eating something");
    }
}
class basisstudent extends student {
    public void sleep()
    {
        System.out.println("no");
    }
}
public class Main {
    public static void main(String[] args)
    {
        basisstudent you = new basisstudent();
        you.dothings();
        you.eatthings();
        you.sleep();
    }
}
Posted by: Guest on August-19-2021
1

what is the need of abstract class

It is helpful if you need to make a generic function that can take a lot of class types as an argument. Eg:


abstract class Shape {
  void area();
  void perimeter();
}


class Rectangle extends Shape {
 int width;
 int height;


 Rectangle(this.width, this.height);


 void area() => this.width * this.height;
 void perimeter() => 2*(this.width + this.height);
}


class Triangle extends Shape {
 int side1;
 int side2;
 int side3;


 Triangle(this.side1, this.side2, this.side3);


  void area() => 0.5 * this.side1 * this.side2 * this.side3;


  void perimeter() => this.side1 + this.side2 + this.side3;
}


void printGeometry(Shape shape) {
 print("The area of this shape is ${shape.area()}";)
 print("The perimeter of this shape is ${shape.perimeter()}";)
}
Posted by: Guest on April-19-2021

Code answers related to "purpose of abstract class in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language