0% found this document useful (0 votes)
7 views

Using Abstract Classes

Uploaded by

C.M Srinivas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Using Abstract Classes

Uploaded by

C.M Srinivas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Using Abstract Classes

• There are situations in which you will want to define a superclass that
declares the structure of a given abstraction without providing a
complete implementation of every method.
• That is, sometimes you will want to create a superclass that only
defines a generalized form that will be shared by all of its subclasses,
leaving it to each subclass to fill in the details. Such a class determines
the nature of the methods that the subclasses must implement.
• abstract keyword in front of the class keyword at the beginning of the
class declaration.
• There can be no objects of an abstract class.
• abstract class cannot be directly instantiated with the new operator.
• we cannot declare abstract constructors, or abstract static methods.
• Any subclass of an abstract class must either implement all of the
abstract methods in the superclass, or be declared abstract itself.
• // A Simple demonstration of abstract.
• abstract class A {
• abstract void callme();
• // concrete methods are still allowed in abstract classes
• void callmetoo() {
• System.out.println("This is a concrete method."); } }
• class B extends A {
• void callme() {
• System.out.println("B's implementation of callme."); } }
• class AbstractDemo {
• public static void main(String[] args) {
• B b = new B();
• b.callme();
• b.callmetoo();
• }
•}

• OUTPUT:
• B's implementation of callme
• This is a concrete method
6.Develop a JAVA program to create an abstract class Shape with abstract methods
calculateArea() and calculatePerimeter(). Create subclasses Circle and Triangle that
extend the Shape class and implement the respective methods to calculate the area and perimeter of each shape.

• public abstract class Shape


• {
• // Abstract methods to calculate area and perimeter
• public abstract double calculateArea();
• public abstract double calculatePerimeter();
• }
• // Circle class, a subclass of Shape
• class Circle extends Shape
• {
• private double radius;
• public Circle(double radius)
• {
• this.radius = radius;
• }
• // Implementing abstract methods
• public double calculateArea()
• {
• return Math.PI * Math.pow(radius, 2);
• }
• public double calculatePerimeter()
• {
• return 2 * Math.PI * radius;
• }
•}
• class Triangle extends Shape
•{
• private double side1, side2, side3;
• // Constructor
• public Triangle(double side1, double side2, double side3)
• {
• this.side1 = side1;
• this.side2 = side2;
• this.side3 = side3;
• }
• // Implementing abstract methods
• public double calculateArea()
• {
• // Using Heron's formula to calculate the area of a triangle
• double s = (side1 + side2 + side3) / 2;
• return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
• }
• public double calculatePerimeter()
• {
• return side1 + side2 + side3;
• }
•}
• class Main {
• public static void main(String[] args) {
• // Creating objects of Circle and Triangle
• Circle circle = new Circle(5.0);
• Triangle triangle = new Triangle(3.0, 4.0, 5.0);
• // Displaying area and perimeter of the Circle
• System.out.println("Circle - Area: " + circle.calculateArea());
• System.out.println("Circle - Perimeter: " +
• circle.calculatePerimeter());
• // Displaying area and perimeter of the Triangle
• System.out.println("Triangle - Area: " +triangle.calculateArea());
• System.out.println("Triangle - Perimeter: " +
• triangle.calculatePerimeter());
• }
• }
• OUTPUT:
• Circle - Area: 78.53981633974483
• Circle - Perimeter: 31.41592653589793
• Triangle - Area: 6.0
• Triangle - Perimeter: 12.0
Using final with Inheritance
• sing final with Inheritance The keyword final has three uses.
• First, it can be used to create the equivalent of a named constant.
• The other two uses of final apply to inheritance. Both are examined
here.
• Using final to Prevent Overriding
• class A {
• final void meth() {
• System.out.println("This is a final method."); } }
• class B extends A {
• void meth() {System.out.println("Illegal!"); } } // ERROR! Can't
//override.
Using final to Prevent Inheritance
• final class A {
//...
}
// The following class is illegal.
class B extends A {
// ERROR! Can't subclass A
//...
}

You might also like