Abstract class java; Everything to understand

abstract class java
abstract class java

We previously told you about the computer network. Here’s we are going to talk about abstract class java. Wanna know everything about abstract class java? Read along!

James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in 1991 and the small team of sun engineers called Green Team. Initially designed for small, embedded systems in electronic appliances like the set-top boxes. It was called Oak and was developed as a part of the green project. James Gosling is the father of Java at Sun Microsystems in 1955 and released the first public implementation as Java 1.0 in 1995.

The language was initially called as Oak because its a symbol of strength and chosen as national tree of many countries like France, Germany, Romania, etc. since Oak was already a registered company, but James Gosling and his team renamed the Oak as Java, this bytecode is a platform-independent code because it can be run on multiple platforms (WORA) Write Once, Run Anywhere and providing no-cost run-times on popular platforms and it also allowed network and file access restrictions.

Web browsers incorporated the ability to run Java applets within web pages and Java quickly become popular. and in 1995, Time magazine called Java on of the ten best products of 1995. and JDK 1.0 released in 1996 after the first release of java there have been many features added to the language. A programming language is a notation for writing programs, which are specifications for a computation. A formal and general-purpose programming language that is used in computer programming to implement algorithms. It’s a language that is object-oriented, class-based and designed to have as some implementation dependencies as possible.

Which comprises a set of instructions that is used to produce many kinds of output, it’s a software platform and Java code can be run on multiple platforms, for example, Windows, Linux, Sun Solaris, Mac/OS, etc. and possibly control external devices such as disk drives, robots, printers and so on. Programming languages differ from natural languages, natural languages are only used for interaction between people while programming languages allow humans to communicate instructions to machines. Most of the programming languages consist of instructions for computers, and many programming languages are written in the imperative form. There are some very important primary principles in the creation of the Java language that it must be robust and secure, it should be interpreted threaded and dynamic, it should be simple, familiar and object-oriented and it must execute with high performance.

Is Java Syntax simple or no?

Java syntax is based on C++ and Java has removed many of the complicated and rarely used features such as explicit pointers and there is no need to remove the unreferenced objects because there is an Automatic Garbage Collection in java. And according to him Standalone, Web, Enterprise, and Mobile are four main applications that can be created by using Java programming.

There are many devices where java is currently used such as;

1. Desktop Applications (acrobat reader, media player, antivirus, etc.) 

2. Web Applications (irctc.co.in, java point.com etc.) 

3. Enterprise Applications (banking applications)

4. Games

5.Smart Card

6. Robotics

7. Embedded System 

8. Mobile

Abstraction:

An abstract is used to implement an abstraction in java and abstract class is not required to have an abstract method at all. Programming languages contain abstractions for manipulating and defining the data structure or controlling the flow of the execution. 

Abstraction in Java:

Clarification about the abstraction in java is the basic thing to understand before learning the concept of abstract class java.

Abstraction shows the only essentials thing to its user and hides all the internal details. In other words, it is a process of showing only functionality to the user and hiding the implementation details. 

For instance, if a person is typing the text and sending a message while that duration that person doesn’t know what kind of internal processing is going on about that message delivery.

There are two main ways to achieve abstraction in java 

1. Abstract class (0-100%)

2. Interface (100%)

Abstract Class Java:

A class that is declared as abstract is an abstract class and it has abstract and abstract methods. The important point to remember in abstract class java; it can have final methods that will force the subclass bot to change the body of the method. it can have static and constructors methods also. it can not be instantiated, it can also have abstract and nonabstract methods. an abstract class must be declared with an abstract keyword.

The shape is the abstract class and its implementation is provided by the circle and rectangular classes. most of the time we have no idea about the implementation class which is actually hidden to the end-user and the other one is an object of the implementation class is provided by the factory method. and that factory method is a method that returns the instance of the class.

An abstract class can be used to provide some necessary implementation of the interface and in such kind of case, the end-user may not be forced to override all methods of the interface. an abstract class can also have a data member, abstract method constructor, method body (non-abstract), method and even main() method.

Class becomes abstract in C++ if the class has at least one pure virtual function. unlike C– in java a separate keyword substructure to make a class abstract.

An example abstract class in java

abstract class Shape {
  int color;
  abstract void draw():
}

An abstract method in Java:

the method without body and with no implementation is known as abstract method. and must be declared in the abstract class. it should be declared abstract if class has an abstract method. there are also some important rules for abstract methods such as; it should be declared abstract if the class has an abstract, method and vice versa is not true, which means an abstract class doesn’t need to have an abstract method compulsory. and abstract methods don’t have anybody they just have method signature. if a regular class extends an abstract class, the class must have to implement all the abstract methods of abstract parent class and it has been declared as well.

Java abstract keyword and abstract class:

A class that contains the abstract keyword in its declaration is an abstract class. An abstract class may or may not declarations abstract methods i.e., methods without a body ( public void get(); ) . to use an abstract class, programmer have to inherit it from the other class and provide implementations to the abstract method in it.

the abstract keyword is a non-access modifier that is used to create abstract class and method and the java keyword is used to achieve the abstraction in java. the main role of abstract class is to contain abstract methods, the method which id declared with abstract keyword and doesn’t have any implementation is an abstract method. if you want to contain a method but you also want the actual implementation of the method to be determined by child classes you can easily declare the method in the parent class as an abstract.

Important point:

Abstract keyword is used to declare the method as abstract.

Abstract methods contain a method signature, but no method body.

You have to place the abstract keyword before the method name in the method declaration.

Instead of the curly braces, an abstract method will have a semicolon (;) at the end.

syntax:

abstract class student
{
  abstract void study();
}

Consequences:

Declaring a method as abstract has two consequences:

  1. Class containing it must be declared as abstract.
  2. Any of the current class must rather override the abstract method or declare itself as abstract.

Do’s and Don’ts of abstract keyword:

Do’s:

  • Declare the abstract method with a throw clause.
  • Declare the local inner class as abstract.
  • An Abstract class can contain overloaded abstract methods.
  • An Abstract class can contain the main method and the final method.
  • If the class extends the abstract class and it must also implement at least one of the abstract methods.
  • An abstract class can contain constructions and static methods.
  • An abstract keyword can only be used with class and method.

Don’ts:

  • An abstract method cannot be synchronized.
  • Cannot declare abstract methods as static.
  • Cannot declare abstract methods as private
  • Cannot use the abstract keyword with the final
  • It does not contain the body if the method of the body is abstract.
  • It cannot be instantiated if a class is abstract
  • An abstract keyword cannot be used with variables and constructors.

Difference between abstract class and interface:

Both the abstract class java and interface are used to achieve the abstraction, where the abstraction methods can be declared. But there are also some points which make differences between abstract class in java and interface. 

 Abstract class java:

 An abstract class java can be extended while using the keyword “extends”.

 The keyword is used to declare an abstract class.

 Abstract class java can have no-final, final, static and non-static variables.

 Abstract class java can have non-abstract and abstract methods.

 An abstract class java can extend another Java class and implement multiple java interface.

 An abstract class java can provide the implementation of an interface.

 Abstract class java does not support multiple inheritances.

An abstract class java can have a class member like private, protected, etc.

Example: public abstract class

shape {
  public abstract void
  draw();
}

Interface:

The interface supports the multiple inheritances.

The Interface has the only final variables and static.

The interface keyword is used to declare the interface.

An interface can be implemented using the keyword “implements”.

An interface can extend another Java interface only.

The interface can have only abstract methods and java have 8, it can have default and static methods also.

An interface can not provide the implementation of an abstract class.

The members of a java interface public by default.

Example:

public interface draw able{
    void draw();
}

Examples of abstract class in java:

In an object-oriented drawing application, any programmer can draw rectangles, circles, Beizer curves, lines, and many other graphic objects. These objects have some certain states such as orientation, position, line color and fill color Other one is the behavior which includes move To, rotates, resizes, draws in common.

some o these states and behavior are the same for all graphics objects such as infill color, position and move to. Others require different implantations like to resize and draw.

All graphic objects must be able to themselves, they just differ only in how they going to do it, this is such a situation for an abstract class java. programmer can take advantage of the similarities and declare all graphic objects to inherit from the same abstract parent object.

Inherit from the graphic objects:

  • rectangular
  • line
  • Beizer
  • circle

The first programmer needs to declare abstract class, graphic objects just to provide member methods and variables that are shared by all subclasses, such as the move to a method and the current position.

The graphic object also declares object abstract methods such as the draw and resize that also need to be implemented by all subclasses but must be implemented in some different ways;

The abstract class look like this;

abstract class graphic object {
    int x, y;
    ....
    void moveTo(int newX, int newY) {
        ...
    }
    abstract void draw();
    abstract ovid resize();
}

Each nonabstract subclass of the graphic objects, such as rectangle and circle, must provide implementations for the resize and draw methods:

class rectangle extends graphic object {
    void draw() {
       ...
    }
    void resize() {
       ...
    }
}

Scenario when abstract class implements an interface:

It was noted that the class that implements an interface must implemented all of the interface’s methods. it is possible. To define a class that does not implement all of the interface’s methods, provided that class is declared to be abstract.

for example:

abstract class X implements Y {
    // implements all but one methods from Y
}

class XX extends X {
   // implements the remaining method in Y
}

Next, you can learn to solve the Tower of Hanoi problem as well.

Imad

I am a Software Engineer with ample experience in making games, websites, mobile apps and augmented reality solutions.

Pin It on Pinterest