- passive income - passive income

Abstract methodology in Java Program

Abstract methodology in Java Program

Abstract categories are classes that contain one or a lot of abstract strategies. the associate abstract methodology may be a method that's declared however contains no implementation. Abstract categories might not be instantiated, and need subclasses to supply implementations for the abstract strategies. Let's examine the associate example of an abstract category, associated with an abstract methodology.

A category that contains the abstract keyword in its declaration is understood as an abstract class.

Abstract categories could or might not contain abstract strategies that are., strategies while not a body ( public void get(); )
But, if a category has a minimum of one abstract methodology, then the category should be declared abstract.
If a category is said abstract it can not be instantiated.
To use an associate abstract category you have got to inherit it from another class, give implementations to the abstract strategies in it.
If you inherit associate abstract category you have got to supply implementations to all or any the abstract strategies in it.
An abstract category may be a class that's lacking some implementation and thus cannot be instantiated on its own while not its full practicality being completed.

Example of an associate abstract category with an abstract (or un-implemented) method:

//example code is in Java
//class should be marked as abstract as a result of it contains the associate abstract methodology
public abstract category Animal-public String name;

    //returns String containing Animal's name
    public String name_toString()come back "Name is: " + name;

    //returns the distinctive noise the Animal makes
    public abstract void noise(); //method wasn't enforced

Abstract methodology in Java Program

Abstract categories are wont to give an overview of a category. not like associate interface (which alone instantiates the methods) it will give some practicality, i.e. some components of the category are enforced and a few parts are simply printed with a way declaration. within the example, higher than, the name_toString methodology is already enforced as a result of this practicality can stay the identical for any category that extends off of Animal class.

To use associate abstract category all you wish to try to is complete its practicality by extending off of it:

public category Extended extends Animal{

     public abstract category Animal-public String name;

        public String name_toString()come back "Name is: " + name;

         public abstract void noise(); //method wasn't enforced
   public category Cat extends Animal


    public static void main(String[] args)doesn't work as a result of category Animal is abstract
       Cat c = new Cat(); //this works as a result of Cat isn't abstract


Suppose we have a tendency to were modeling the behavior of animals, by making a category hierarchy that started with a base class known as Animal. Animals are capable of doing various things like flying, creating by removal and walking, however, there are some common operations furthermore as feeding and sleeping. Some common operations are performed by all animals, however in a very totally different manner furthermore. once an associate operation is performed in a very totally different manner, it's an honest candidate for associate abstract methodology (forcing subclasses to supply a custom implementation). Let's examine a really primitive Animal base category, that defines an associate abstract methodology for creating a sound (such as a dog barking, a cow mooing, or a pig oinking).

public abstract Animal
one thing with food... 

   public void sleep(int hours)
   a thousand milliseconds * sixty seconds * hour * hours
  Thread.sleep ( a thousand * sixty * 60 * hours);
 catch (InterruptedException ie)  

   public abstract void makeNoise();
Note that the abstract keyword is employed to denote each associate abstract methodology and an abstract category. Now, any animal that desires to be instantiated (like a dog or cow) should implement the makeNoise methodology - otherwise, it's not possible to make an associate instance of that category. Let's examine a Dog and Cow taxon that extends the Animal class.

public Dog extends Animal


public Cow extends Animal

Now you'll be speculative why not declare associate abstract category as an interface, and have the Dog and Cow implement the interface. certain you'll - however you'd conjointly must implement the eat and sleep strategies. By victimization abstract categories, you'll be able to inherit the implementation of different (non-abstract) strategies. You can't try this with interfaces - associate interface cannot give any methodology implementations
  1. abstract class GraphicObject {
  2. int x, y;
  3. ....
  4. void moveTo(int newX, int newY) {
  5. ... // implemented here
  6. }
  7. abstract void draw(); // to be implemented by subclass
  8. abstract void resize(); // to be implemented by subclass
  9. }
  10. class Circle extends GraphicObject {
  11. void draw() {
  12. ...
  13. }
  14. void resize() {
  15. ...
  16. }
  17. }
  18. class Rectangle extends GraphicObject {
  19. void draw() {
  20. ...
  21. }
  22. void resize() {
  23. ...
  24. }
  25. }
Abstract methodology in Java Program Abstract methodology in Java Program Reviewed by Raj Aryan on April 10, 2019 Rating: 5

1 comment:

Powered by Blogger.