Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Oct 2015
    Location
    India
    Posts
    177

    instantiate an Abstract class in Java?

    Hello Dear,

    Please Tell Me Is it possible to instantiate an Abstract class in Java?

  2. #2
    Senior Member dennis123's Avatar
    Join Date
    Apr 2013
    Location
    Bangalore
    Posts
    3,627
    Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract

  3. #3
    Senior Member dennis123's Avatar
    Join Date
    Apr 2013
    Location
    Bangalore
    Posts
    3,627
    abstract class my {
    public void mymethod() {
    System.out.print("Abstract");
    }
    }

    class poly {
    public static void main(String a[]) {
    my m = new my() {};
    m.mymethod();
    }
    }
    for your kind of information

  4. #4
    Registered User
    Join Date
    Mar 2019
    Location
    Ahmedabad
    Posts
    180
    • An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
    • An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
      Code:
      abstract void moveTo(double deltaX, double deltaY);
    • If a class includes abstract methods, then the class itself must be declared abstract, as in:
      Code:
      public abstract class GraphicObject {
         abstract void draw();
      }
    • When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.


    Example::

    Code:
    abstract class Vehicle {
      public abstract void runMotor();
    }
      
    class SUV extends Vehicle {
      public void runMotor() {
        System.out.println( this.getClass().getName() + ": Need more gas");
      }
    }
      
    class Hybrid extends Vehicle {
      public void runMotor() {
        System.out.println( this.getClass().getName() + ": Woohoo, let's go!");
      }
    }
      
    public class LetsGoForARide {
      public static void main( String[] args ) {
        Vehicle[] vehicles = new Vehicle[2];
        vehicles[0] = new SUV();
        vehicles[1] = new Hybrid();
        for( int i = 0; i < vehicles.length; ++i ) {
          vehicles[i].runMotor();
        }
      }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

  Find Web Hosting      
  Shared Web Hosting UNIX & Linux Web Hosting Windows Web Hosting Adult Web Hosting
  ASP ASP.NET Web Hosting Reseller Web Hosting VPS Web Hosting Managed Web Hosting
  Cloud Web Hosting Dedicated Server E-commerce Web Hosting Cheap Web Hosting


Premium Partners:


Visit forums.thewebhostbiz.com: to discuss the web hosting business, buy and sell websites and domain names, and discuss current web hosting tools and software.