Skillyfy provides best Java Training in DELHI NCR. We have a team of experienced Java
professionals and trainers who have vested many years in this ever-growing IT industry. Through our best Java Training course in Delhi you’ll learn all of the Java fundamentals. Knowing Java opens a great deal of doors for a developer. This Java blog introduces basic concepts and features of Abstract class and describes its all usage in Java. Consider if you want to define a superclass that declares the structure to a class without providing a complete implementation of every method. So create a superclass that only defines a generalized form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details. Consider the class Shape. It has method like calculateArea() but we don’t know for which shape we need to calculate area as the shape Square, Triangle and Rectangle will have different calculation formula. In this case, you want some way to ensure that a subclass does, indeed, override calculateArea() method and give body according to the specific shape. Java’s solution to this problem is the abstract method. In this case you have to create a class called Shape and define a method calculateArea() which will be an incomplete method. class Shape{ void calculateArea(); // incomplete method } In Java if we have any incomplete method we have to declare that method as Abstract method and if any class contain even a single abstract method in that case you have to declare the class also as Abstract. abstract class Shape{ abstract void calculateArea(); // incomplete method } // A Simple demonstration of abstract. abstract class MyClass { abstract void absMethod(); // concrete methods are still allowed in abstract classes void completeMethod() { System.out.println("This is a compete method."); } } class B extends MyClass { void absMethod() { System.out.println("B's implementation of absMethod()."); } } class AbstractDemo { public static void main(String args[]) { B b = new B(); b.absMethod(); b.completeMethod(); } } If you have noticed that no objects of class A was created in the program. It is not possible to instantiate an abstract class. One other point: class MyClass implements a concrete method called completeMethod( ). This is perfectly acceptable. Abstract classes can include as much implementation as required. Things to remember
This article is contributed by Skillyfy. Skillyfy provides best Java Training in DELHI, Ethical Hacking Training in Delhi NCR with 100% complete practical demonstrations. Please call on 7827814720 or email [email protected] if you want to take more information about the topic discussed above or any other courses. Happy Learning !!
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories
All
|