Home | Java - Abstract Class

Chapter: Web or internet Programming : JAVA

Java - Abstract Class

Sometimes you will want to 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.

 

Abstract Class:

 

Sometimes you will want to 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. Such a class determines the nature of the methods that the subclasses must implement. You can require that certain methods be overridden by subclasses by specifying the abstract type modifier. These methods are sometimes referred to as subclass responsibility because they have no implementation specified in the superclass. Thus, a subclass must override them—it cannot simply use the version defined in the superclass.

 

 

To declare an abstract method, use this general form:

 

 

abstract type name(parameter-list);

 

 

Any class that contains one or more abstract methods must also be declared abstract. To declare a class abstract, you simply use the abstract keyword in front of the class keyword at the beginning of the class declaration. An abstract class cannot be directly instantiated with the new operator. Such objects would be useless, because an abstract class is not fully defined. Also, you cannot declare abstract constructors, or abstract static methods. Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be itself declared abstract.

 

 

abstract class A5{

 

abstract void callme();

 

void callmetoo()

{

 

System.out.println("this is an example for abstract class ");

 

}

}

 

class B5 extends A5{

 

void callme(){

 

System.out.println("this ia an implementation of abstract function");

}

 

}

 

public class abstractclass {

public static void main(String args[])

 

{

B5 bobj=new B5();

 

bobj.callme(); bobj.callmetoo();

} }

 

Uses of Final Keyword: Using final with Inheritance

 

The keyword final has three uses. First, it can be used to create the equivalent of a named

 

constant.

 

Using final to Prevent Overriding

 

While method overriding is one of Java’s most powerful features, there will be times when you will want to prevent it from occurring. To disallow a method from being overridden, specify final as a modifier at the start of its declaration

 

Using final to Prevent Inheritance

 

Sometimes you will want to prevent a class from being inherited. To do this, precede the class declaration with final. Declaring a class as final implicitly declares all of its methods as final, too.

 

// Named Constant final int x=4;

 

//to prevent overriding class A3{

 

final void meth2() {

 

System.out.println("This is final method");

 

}

 

}

 

 

class B3 extends A3{

 

void meth(){

 

System.out.println("this is not possible");

 

}

 

}

 

// to prevent inheritance

final class A4{     

-------------------------  }    

class A4 extends B4{    

// this is also not possible       

}
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : JAVA : Java - Abstract Class |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.