A class that is declared using “abstract” keyword is known as abstract class. It can have abstract methods(methods without body) as well as concrete methods (regular methods with body). A normal class(non-abstract class) cannot have abstract methods. An abstract class cannot be instantiated, which means you are not allowed to create an object of it.
Let us assume we have a class Animal that has a method sound() and the subclasses of it like Dog, Lion, Horse, Cat etc. Since the animal sound differs from one animal to another, there is no point to implement(write code) this method in parent class. This is because every child class must override this method to give its own implementation details, like Lion class will say “Roar” in this method and Dog class will say “Woof” and so on.
So when we know that all the animal child/sub classes will and should override this method, then there is no point to implement this method in parent class. Thus, making this method abstract would be the good choice as by making this method abstract we force all the sub classes to implement this method( otherwise you will get compilation error), also we need not to give any implementation to this method in parent class.
Abstract class declaration
An abstract class outlines the methods but not necessarily implements all the methods.
Syntax:
//Declaration using abstract keyword
abstract class A
{//This is abstract method
abstract void myMethod();
//This is concrete method with body
//This is concrete method with body
void anotherMethod()
{
//Does something
}
}
{
//Does something
}
}
Example on Vehicle:
abstract class Vehicle
{
int nooftires;
abstract void start();
}
class Car extends Vehicle
{
void start()
{
System.out.println("Car Start with Keys");
}
}
class Scotter extends Vehicle
{
void start()
{
System.out.println("Scotter Start with Kick");
}
public static void main(String[] args)
{
//Vehicle v = new Vehicle(); abstract class canot create objects
Car c = new Car();
c.start();
Scotter s = new Scotter();
s.start();
}
}
Output:
C:\Users\KANAKA RAJU\Desktop\java 9-5-22>javac Vehicle.java
C:\Users\KANAKA RAJU\Desktop\java 9-5-22>java Vehicle
Exception in thread "main" java.lang.NoSuchMethodError: main
C:\Users\KANAKA RAJU\Desktop\java 9-5-22>java Scotter
Car Start with Keys
Scotter Start with Kick
Ex:
abstract class Animal
{
//abstract method
public abstract void sound();
}
//Dog class extends Animal class
public class Dog extends Animal
{
public void sound()
{
System.out.println("Woof");
}
public static void main(String args[])
{
Animal obj = new Dog();
obj.sound();
}
}
Output:
C:\Users\Student\Desktop\java prg>javac Dog.java
C:\Users\Student\Desktop\java prg>java Dog
WoofWhy can’t we create the object of an abstract class?
Ex:
Ex:
Because these classes are incomplete, they have abstract methods that have no body so if java allows you to create object of this class then if someone calls the abstract method using that object then What would happen? There would be no actual implementation of the method to invoke. Also because an object is concrete. An abstract class is like a template, so you have to extend it and build on it before you can use it.
abstract class AbstractDemo
{
public void myMethod()
{
System.out.println("Hello");
}
abstract public void anotherMethod();
}
public class Demo extends AbstractDemo
{
public void anotherMethod()
{
System.out.print("Abstract method");
}
public static void main(String args[])
{
//error: You can't create object of it
AbstractDemo obj = new AbstractDemo();
obj.anotherMethod();
}
}
Output:C:\Users\Student\Desktop\java prg>javac Demo.java
Demo.java:18: AbstractDemo is abstract; cannot be instantiated
AbstractDemo obj = new AbstractDemo();
^
1 error
Abstract Class and Method
abstract class MyClass
{
public void disp()
{
System.out.println("Concrete method of parent class");
}
abstract public void disp2();
}
class Demo extends MyClass
{
/* Must Override this method while extending MyClas */
public void disp2()
{
System.out.println("overriding abstract method");
}
public static void main(String args[])
{
Demo obj = new Demo();
obj.disp2();
}
}
Output:
Abstract Method
A method that doesn't have its body is known as an abstract method. We use the same abstract keyword to create abstract methods.
C:\Users\Student\Desktop\java prg>javac Demo.java
C:\Users\Student\Desktop\java prg>java Demo
overriding abstract method
Abstract Method
A method that doesn't have its body is known as an abstract method. We use the same abstract keyword to create abstract methods.
Syntax:
abstract void display();
abstract void display();
Ex:
Ex: Abstraction
abstract class Language
{
// method of abstract class
public void display()
{
System.out.println("This is Java Programming");
}
}
class Main extends Language
{
public static void main(String[] args)
{
// create an object of Main
Main obj = new Main();
// access method of abstract class
// using object of Main class
obj.display();
}
}
Output
C:\Users\Student\Desktop\java prg>javac Main.java
C:\Users\Student\Desktop\java prg>java Main
This is Java Programming
abstract class Animal
{
abstract void makeSound();
}
class Dog extends Animal
{
// implementation of abstract method
public void makeSound()
{
System.out.println("Bark bark.");
}
}
class Cat extends Animal
{
// implementation of abstract method
public void makeSound()
{
System.out.println("Meows ");
}
}
class Driver
{
public static void main(String[] args)
{
Dog d1 = new Dog();
d1.makeSound();
Cat c1 = new Cat();
c1.makeSound();
}
}
Output:C:\Users\Student\Desktop\java prg>javac Driver.java
C:\Users\Student\Desktop\java prg>java Driver
Bark bark.
Meows
No comments:
Post a Comment