Answers for "abstraction example in java"

45

abstract class in java

Sometimes we may come across a situation where we cannot provide 
implementation to all the methods in a class. We want to leave the 
implementation to a class that extends it. In such case we declare a class
as abstract.To make a class abstract we use key word abstract. 
Any class that contains one or more abstract methods is declared as abstract. 
If we don’t declare class as abstract which contains abstract methods we get 
compile time error.
  
  1)Abstract classes cannot be instantiated
  2)An abstarct classes contains abstract method, concrete methods or both.
  3)Any class which extends abstarct class must override all methods of abstract
    class
  4)An abstarct class can contain either 0 or more abstract method.
Posted by: Guest on November-30-2020
6

abstraction in java

Abstraction is defined as hiding internal implementation and showing only 
necessary information.
// abstract class
abstract class Addition
{
   // abstract methods
   public abstract int addTwoNumbers(int number1, int number2);
   public abstract int addFourNumbers(int number1, int number2, int number3, int number4);
   // non-abstract method
   public void printValues()
   {
      System.out.println("abstract class printValues() method");
   }
}
class AbstractMethodExample extends Addition
{
   public int addTwoNumbers(int number1, int number2)
   {
      return number1 + number2;
   }
   public int addFourNumbers(int number1, int number2, int number3, int number4)
   {
      return number1 + number2 + number3 + number4;
   }
   public static void main(String[] args)
   {
      Addition add = new AbstractMethodExample();
      System.out.println(add.addTwoNumbers(6, 6));
      System.out.println(add.addFourNumbers(8, 8, 3, 2));
      add.printValues();
   }
}
Posted by: Guest on October-23-2020
3

abstraction java

bstraction: Hiding the implementation details, 
concentrating on essentials things, 
without worrying about the details 
■ In java, abstraction is achieved via abstract class or interface 
■ Abstraction cannot exist without inheritance 

3.1. Abstract class a class that's meant to be inherited 
(cannot be private or final)
■ To create an abstract class: add keyword abstract 
before the class declaration 
■ A class that we cannot create object 
when abstract class extended by regular class: 
we MUST override all the abstract methods of super class
when abstract class extended by abstract class: 
no need to override any abstract methods

abstract class vs non-abstract class:
NON-ABSTRACT CLASS:
cannot have: abstract methods
can have: constructor, instance method, static method, 
instance block, static block, instance variable, static variable
ABSTRACT CLASS:
can have: constructor, instance method, static method, instance block,
static block, instance variable, static variable...
ABSTRACT METHOD: method without implementation, meant to be override 
(cannot create object, cannot be final,static,private)

FRAMEWORK EXAMPLE:
In my framework I have achieved abstraction by using collections 
or Map, because it’s all interface. Most of the cases I come 
across using List. If we want to access elements frequently 
by using index, List is a way to go. ArrayList provides 
faster access if we know index. If we want to store elements and 
want them to maintain an order, List is a better choice. 
i) List webs = driver.getWindowHandles();    
=>create a list first to store web URLs in list
ii)To handle dynamic elements store it in the list and identify by index
List all = driver.findElements(By.tagname(“”));
In my framework I follow POM and had situations where some pages 
shared similar actions that were similar but worked slightly 
different, so I was able to use abstraction to define those 
actions and implement them in each page according to what was needed 
for that webpage
Posted by: Guest on May-28-2021
3

abstraction in java

Abstraction is nothing but the quality of dealing with ideas rather than 
events. It basically deals with hiding the internal details and showing 
the essential things to the user.
Posted by: Guest on June-22-2020
1

Abstraction in java

// abstraction in java with example
// abstract class
abstract class Addition
{
   // abstract methods
   public abstract int addTwoNumbers(int number1, int number2);
   public abstract int addFourNumbers(int number1, int number2, int number3, int number4);
   // non-abstract method
   public void printValues()
   {
      System.out.println("abstract class printValues() method");
   }
}
class AbstractMethodExample extends Addition
{
   public int addTwoNumbers(int number1, int number2)
   {
      return number1 + number2;
   }
   public int addFourNumbers(int number1, int number2, int number3, int number4)
   {
      return number1 + number2 + number3 + number4;
   }
   public static void main(String[] args)
   {
      Addition add = new AbstractMethodExample();
      System.out.println(add.addTwoNumbers(6, 6));
      System.out.println(add.addFourNumbers(8, 8, 3, 2));
      add.printValues();
   }
}
Posted by: Guest on November-23-2020
0

abstraction example in java

In my framework I have achieved abstraction 
by using collections or Map, because it’s all interface. 
  Most of the cases I come   across using List. 
  If we want to access elements frequently by using index, 
List is a way to go. ArrayList provides faster access if we know index. 
  If we want to store elements and want them to maintain an order, 
List is a better choice. 
i) List webs = driver.getWindowHandles(); 
=>create a list first to store web URLs in list
ii) findElements evaluates multiple elements 
so therefore will assigned to List 
iii) To handle dynamic elements store it in the list and identify by index: 
List all = driver.findElements(By.tagname(“”));
In my framework I follow POM and had situations 
where some pages shared similar actions that were similar 
but worked slightly different, so I was able to use abstraction 
to define those actions and implement them in each page according 
to what was needed for that webpage
Posted by: Guest on May-28-2021

Code answers related to "abstraction example in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language