How and when to use an abstract class

abstract class c#
java abstract class vs interface
abstract class c++
when to use abstract class and interface in java with real time example
abstract class in android
java abstract class constructor
abstract class in oops
abstract method in java

This is my test program in Java. I want to know how much abstract class is more important here and why we use abstract class for this.

Is it a mandatory or is it best method; if so how?

class Shape1 {
    int i = 1;
    void draw() {
        System.out.println("this is shape:" + i);
    }
}

class Shape2 {
    int i = 4;
    void draw() {
        System.out.println("this is shape2:" + i);
    }
}


class Shape {
    public static void main(String args[]) {
        Shape1 s1 = new Shape1();
        s1.draw();

        Shape2 s2 = new Shape2();
        s2.draw();
    }
}

You'd use an abstract class or interface here in order to make a common base class/interface that provides the void draw() method, e.g.

abstract class Shape() {
  void draw();
}

class Circle extends Shape {
   void draw() { ... }
}

...

Shape s = new Circle();
s.draw();

I'd generally use an interface. However you might use an abstract class if:

  1. You need/want to provide common functionality or class members (e.g. the int i member in your case).
  2. Your abstract methods have anything other than public access (which is the only access type allowed for interfaces), e.g. in my example, void draw() would have package visibility.

Java - Abstraction, An abstract class is a class that is declared abstract — it may or may not include abstract methods. Abstract classes cannot be instantiated, but  We use the same keyword abstract to create abstract methods. An abstract method is declared without an implementation. For example, abstract void makeSound(); Here, makeSound () is an abstract method. The body of makeSound () is replaced by ;. It's important to note that, only an abstract class can contain abstract methods. If we include

An abstract class has an "is-a" type relationship with your subclasses. So for instance, you could have an abstract class Shape which has stuff any shape has (like a draw function), and then a class SquareShape. Every squareshape is a shape, but not all shapes are squareshapes.

In you example you have 2 shape-types, and a class that has 2 instances of these. That is not a relationship you should define with abstract I think.

You might want to do something with your names though, because this is a rather small example, it's hard to see the real implications of the files, and how they should work.

When to Use Abstract Class and Interface, You'd use an abstract class or interface here in order to make a common base class/interface that provides the void draw() method, e.g. abstract class Shape()  Consider using abstract classes if any of these statements apply to your situation: You want to share code among several closely related classes. You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).

An abstract class is a class, which has at least one method not implemented, or the keyword abstract. For example, an abstract method may look like this:

public abstract String myMethod(String input);

(note that the method ends with a semi-colon).

And a class may look like this:

public abstract class MyClass {

    public abstract String myMethod(String input);

    public String anotherMethod(String input) {
        return intput + " additional text";
    }
}

An abstract class cannot be instantiated. Abstract classes require a subclass to implement the missing behaviour so that it can be instantiated.

The main goal of an abstract class is to provide shared implemenentation of common behaviour - promoting the reuse of code.

In Java the same effect can be achieve by using a composition of classes instead of inheritance from broadly defined abstract classes. This allows more modular, function specific classes promoting code reuse, that in turn increase maintainability.

My advice would be to use abstract class only when strictly necessary, and in particular avoid using it as a trick bag full of all sorts of functionality.

In Scala one would use traits, which are an elegant way to solve this. It does however, require a lot of attention to get it right through.

How and when to use an abstract class, An abstract class is a class that is declared abstract —it may or may not include abstract methods. Which should you use, abstract classes or interfaces? An abstract class may have static fields and static methods. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.

sample example for using abstract class in java.

package use_of_abstract;

abstract class Shapes 
{
   int i=1;

   abstract void draw();
   abstract void color(String mycolor);

   //not an abstract method
   void fill()
   {
      System.out.println("Non-Abstract Method -> Fill"); 
   }

   //not an abstract method
   String anotherMethod(String input) 
   {
       return input + " additional text";
   }

}

package use_of_abstract;

public class Shape_One extends Shapes 
{
    int i=1;

    @Override
    void draw() 
    {
        System.out.println("This is Shape One:"+i);
    }

    @Override
    void color(String mycolor) 
    {
        System.out.println("This is Shape One:"+mycolor);

    }

    @Override
    String anotherMethod(String anotherMethod) 
    {
        System.out.println("This is Shape One:"+anotherMethod);
        return anotherMethod;

    }

}

package use_of_abstract;

public class Shape_Two extends Shapes
{
    int i=2;

    @Override
    void draw() 
    {
        System.out.println("This is Shape Two :"+i);
    }

    @Override
    void color(String mycolor) 
    {
        System.out.println("This is Shape Two Color:"+mycolor);
    }

    @Override
    String anotherMethod(String anotherMethod) 
    {
        System.out.println("This is Shape Two:"+anotherMethod);
        return anotherMethod;

    }

}

package use_of_abstract;

import java.awt.Color;

public class Shape_Main 
{

    public static void main(String args[])
    {
        Shape_One s1;
        Shape_Two s2;

        s1=new Shape_One();
        s2= new Shape_Two();

        s1.draw();
        s2.draw();

        s1.fill();
        s2.fill();

        s1.color("Blue");
        s2.color("Green");


        s1.anotherMethod("HELLO..............Its Another Method 1");
        s2.anotherMethod("HELLO..............Its Another Method 2");


    }   
}

Abstract Methods and Classes (The Java™ Tutorials > Learning the , We use cookies to ensure you have the best browsing experience on our website​. Unlike C++, in Java, a separate keyword abstract is used to make a class 1) Like C++, in Java, an instance of an abstract class cannot be created, we can  If a class has at least one abstract method, then the class must be declared abstract. To use an abstract class, we must create a class that extends the abstract class (inheritance) and provide implementations for all abstract methods. Java does not support multiple inheritance so we are only allowed to extend one class (abstract or not).

Abstract class, as its name suggest is abstract.

Abstract class doesn't talk about the implementation part. In fact, we extend the abstract class to provide the implementation for its abstract methods. It can also have non-abstract methods.

Check here : Use of Abstract Class in Java

EDIT :

Sample Code :

abstract class Shapes {

 int i=1;
 abstract void draw(); 

 // Remember : Non-abstract methods are also allowed 
 void fill() {
     System.out.println("Non abstract method - fill");
 }
}

class Shape1 extends Shapes {

 int i=1;
 void draw(){
 System.out.println("This is Shape 1:"+i);
 }
}

class Shape2 extends Shapes {
    int i=2;
    void draw() {
        System.out.println("This is Shape 2:"+i);
    }
}

class Shape {

public static void main(String args[])
       {
        Shape1 s1 = new Shape1();
        s1.draw();                     // Prints This is Shape 1:1

        Shape2 s2 = new Shape2();
        s2.draw();                     // Prints This is Shape 2:2
       }
  }

Abstract Classes in Java, A class that is declared using “abstract” keyword is known as abstract class. It can have abstract methods(methods without body) as well as concrete methods  Abstract classes provide you the flexibility to have certain concrete methods and some other methods that the derived classes should implement. On the other hand, if you use interfaces, you would need to implement all the methods in the class that extends the interface. An abstract class is a good choice if you have plans for future expansion.

Abstract Class in Java with example, abstract class mainly use to restrict the object creation of that class. Reply. Pulkit Sharma says: July 30, 2019 at 6:18 am. if  Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class must provide definition to the pure virtual function, otherwise they will also become abstract class. Characteristics of Abstract Class

Abstract Class in Java, Interface Class. We know that an interface is defined by the interface keyword and all methods are abstract. All methods declared in an interface must be public;​  Rule: If you are extending an abstract class that has an abstract method, you must either provide the implementation of the method or make this class abstract. Another real scenario of abstract class. The abstract class can also be used to provide some implementation of the interface. In such case, the end user may not be forced to override all the methods of the interface.

Understanding the Use of Interface and Abstract Classes, The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to  An abstract class in C# provides a blueprint of what its derived classes must implement before they can use it. An abstract class contains abstract members including abstract methods, abstract properties, abstract indexers, and abstract events. All abstract members of a class must be implemented by its derived classes. Here are the key

Comments
  • I would say your example points more to the usage of an interface than an abstract. It is one of the typical examples to show the usage of an interface: diferent shapes (square, circle, etc) can be drawn.
  • Abstract classes don't necessarily need to have abstract methods... you could define an abstract class which has all of its methods implemented, for example.
  • This is not true. an abstact class can have tons of impelementations. It can also have abstract methods that look to the extending class to implement, but it certainly can have implementation. Interfaces don't have implementation, abstract classes can (yes, you can have an abstract class with only abstract methods, but it doesn't have to be that way). You can't have an instance of an abstract class ofcourse.