Abstract methods:
An abstract
method is a method that is declared
without an implementation, the declaration of an abstract method ends with a
semicolon and the method with the keyword abstract.
public abstract class
Animal {
public abstract void
sound();
}
abstract method
can’t be
native
final
static
synchronized
private
strictfp
Abstract class:
An abstract
class is class which contains the zero or more abstract method. if class is
declared as abstract it can’t instantiated.
package
com.jsl.core;
public abstract class
Animal {
public Animal(String
name) {
this.name=name;
}
public abstract
String sound();
public
String toString(){
return name;
}
String name;
}
Animal class contains
the concrete method as well abstract method. We can’t instantiate the Animal
class.
The
class can be declared as abstract, without abstract method. So it’s restricting
not to create object.
public abstract class MathOperation {
public double
multiply(double amount,double price){
return
amount*price;
}
}
If you want to access
the multiply method another class
that class must extends the MathOperation.
public class
UserOperations extends MathOperation {
private double a=10,b=20;
public void
showResult(){
System.out.println(multiply(a, b));
}
}
An abstract
class can implement the interface.
package
com.jsl.core;
interface
Game{
public void
play();
public void
makeSound();
}
public abstract class Car
implements Game {
@Override
public void
play() {
------
------
}
}
The abstract method
can be called from the concrete method of the abstract calls.
package com.jsl.core;
abstract class One{
public abstract void print(int res);
public void sum(int a,int b){
int res=a+b;
print(res);
}
}
class Two extends One{
@Override
public void print(int res) {
System.out.println("The result is : "+res);
}
}
public class AbstractDemo {
public static void main(String[] args) {
One o=new Two();
o.sum(20, 30);
}
}
package com.jsl.core;
abstract class One{
public abstract void print(int res);
public void sum(int a,int b){
int res=a+b;
print(res);
}
}
class Two extends One{
@Override
public void print(int res) {
System.out.println("The result is : "+res);
}
}
public class AbstractDemo {
public static void main(String[] args) {
One o=new Two();
o.sum(20, 30);
}
}
Example:
package
com.jsl.core;
public abstract class
Car{
public void
start(){
play();
}
public void
play(){
System.out.println("The
play method ");
}
}
If the class
extends the abstract class, then that class must implements all the methods
otherwise that class will become the abstract class.
package
com.jsl.core;
abstract class
One{
abstract public void
start();
public abstract void
play();
public abstract void
stop();
}
public abstract
class Two extends One{
@Override
public void
play() {
}
@Override
public void
stop() {
}
}
The abstract class
can’t create object but it contains the constructor.
package
com.jsl.core;
abstract class
One{
public
One(){
System.out.println("The
one constructor");
}
}
class Two
extends One{
public
Two(){
System.out.println("The
Two constructor");
}
}
class
Three {
public static void
main(String[] args) {
Two obj=new Two();
}
}
No comments:
Post a Comment