TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Friday, 27 December 2019

abstract class in java | purpose of abstract class in java in hindi

Abstraction in Java

Data Abstraction is the property by virtue of which only the essential details are displayed to the user.The trivial or the non-essentials units are not displayed to the user. Ex: A car is viewed as a car rather than its individual components.
Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details.The properties and behaviors of an object differentiate it from other objects of similar type and also help in classifying/grouping the objects.
        

                  Abstract classes and Abstract methods :

1.    An abstract class is a class that is declared with abstract keyword.
2.    An abstract method is a method that is declared without an implementation.
3.    An abstract class may or may not have all abstract methods. Some of them can be concrete methods
4.    A method defined abstract must always be redefined in the subclass,thus making overriding compulsory OR either make subclass itself abstract.
5.    Any class that contains one or more abstract methods must also be declared with abstract keyword.
6.    There can be no object of an abstract class.That is, an abstract class can not be directly instantiated with the new operator.
7.    An abstract class can have parametrized constructors and default constructor is always present in an abstract class.

                         Advantages of Abstraction

1.    It reduces the complexity of viewing the things.
2.    Avoids code duplication and increases reusability.
3.    Helps to increase security of an application or program as only important details are provided to the user.
 
purpose of abstract class in java , difference between interfa java with exampledifference between interface and abstract class in java with example
Abstracting in java

Example:

// Java program to illustrate the
// concept of Abstraction
abstract class Shape 
{
    String color;
      
    // these are abstract methods
    abstract double area();
    public abstract String toString();
      
    // abstract class can have constructor
    public Shape(String color) {
        System.out.println("Shape constructor called");
        this.color = color;
    }
      
    // this is a concrete method
    public String getColor() {
        return color;
    }
}

class Circle extends Shape
{
    double radius;
      
    public Circle(String color,double radius) {
  
        // calling Shape constructor
        super(color);
        System.out.println("Circle constructor called");
        this.radius = radius;
    }
  
    @Override
    double area() {
        return Math.PI * Math.pow(radius, 2);
    }
  
    @Override
    public String toString() {
        return "Circle color is " + super.color + 
                       "and area is : " + area();
    }
      
}
class Rectangle extends Shape{
  
    double length;
    double width;
      
    public Rectangle(String color,double length,double width) {
        // calling Shape constructor
        super(color);
        System.out.println("Rectangle constructor called");
        this.length = length;
        this.width = width;
    }
      
    @Override
    double area() {
        return length*width;
    }
  
    @Override
    public String toString() {
        return "Rectangle color is " + super.color + 
                           "and area is : " + area();
    }
  
}
public class Test 
{
    public static void main(String[] args)
    {
        Shape s1 = new Circle("Red", 2.2);
        Shape s2 = new Rectangle("Yellow", 2, 4);
          
        System.out.println(s1.toString());
        System.out.println(s2.toString());
    }
}

 Output:

Shape constructor called
Circle
Shape
Rectangle
Circle color is Redand area is:15.205308443
Rectangle color is yellowand area is:8.0

No comments:

Post a Comment