Abstract Class in Java

Тип статьи:
Авторская
  1. Abstract class in Java is related to interface except that it can include error method implementation. This class can become an abstract method without a body and it can have plans with implementation also. Core Java is considered as one of the pure languages because it does not have multiple articles like Operator overloading, Multiple inheritances, pointers, and Specific memory allocation. Want to learn Core Java Training in Chennai? Walk into FITA, one of the Best leading institutions for Core Java Training Institutes in Chennai.

Abstract keyword is used to generate an ideal class and design. Abstract classes can't be instantiated. An abstract class is frequently applied to implement a base for subclasses to reach and complete the abstract designs and override or use the completed designs in the abstract class.

An Example of an Abstract Class in Java.

package com.abstract.design;

// abstract class

public abstract class Person {

private String name;

private String gender;

public Person (String nm, String gen) {

this.name = nm;

this.gender = gen;

}

// abstract method

public abstract void work ();

@Override

public String toString () {

return «Name =» + this.name + ":: Gender =" + this.gender;

}

public void changeName (String newName) {

this.name = newName;

}

}

package com.abstract.design;

public class Employee extends Person {

private int empId;

public Employee (String nm, String gen, int id) {

super (nm, gen);

this.empId = id;

}

@Override

public void work () {

if (empId == 0) {

System.out.println («Not working»);

} else {

System.out.println («Working as employee !!»);

}

}

public static void main (String args []) {

// coding in terms of abstract classes

Person student = new

Employee («jiju», «Female», 0);

Person employee = new

Employee («joju», «Male», 123);

student.work ();

employee.work ();

// using method implemented in abstract class — inheritance

employee.changeName («Joju Roy»);

System.out.println (employee.toString ());

}

}

Important Points of Abstract class in Java


  • Abstract keyword is used to generate an abstract class in java.

  • 1.Abstract class in java cannot be instantiated.

  • 2.We can use the abstract keyword to generate an abstract method, an abstract method doesn't have a body.

  • 3.If a class have abstract methods, then the class should also be difficult using abstract keyword, else it will not collect.

  • 4.It's not required for an abstract class to have an abstract method. We can mark a class as abstract even if it doesn't hold any complex methods.

  • 5.If an abstract class doesn't possess any method implementation, it's better to apply the interface because java doesn't support multiple class inheritance.

  • 6.The subclass of abstract class in java must complete all the general rules except the subclass is also an abstract class.

  • 7.All the rules in an interface are involved completely except the interface methods are static or default. Static methods and default methods in interfaces are combined in Java 8, for more details read Java 8 interface developments.

  • 8.Java Abstract class can complete interfaces without even giving the implementation of interface designs.

  • 9.Java Abstract class is used to implement common method implementation to all the subclasses or to implement a default implementation.

  • 10.We can run an abstract class in java like any other class if it has a main () method.

FITA is the best leading institution for Core Java Training Institutes in Chennai. Our trainers are with real-time experience in the IT industry, they are from leading MNC's companies for more than a decade.

красный зелёный голубой