0% found this document useful (0 votes)
2 views

Abstract class, Method overring ,final keyword

The document explains the concept of abstract classes and methods in Java, highlighting their characteristics and rules, including the inability to instantiate abstract classes and the requirement for subclasses to implement abstract methods. It also covers method overriding, its rules, and the significance of the final keyword in Java, which restricts variable, method, and class modifications. Examples are provided to illustrate these concepts in practice.

Uploaded by

puppy567567
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Abstract class, Method overring ,final keyword

The document explains the concept of abstract classes and methods in Java, highlighting their characteristics and rules, including the inability to instantiate abstract classes and the requirement for subclasses to implement abstract methods. It also covers method overriding, its rules, and the significance of the final keyword in Java, which restricts variable, method, and class modifications. Examples are provided to illustrate these concepts in practice.

Uploaded by

puppy567567
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Abstract class in Java

o An abstract class must be declared with an abstract keyword.


o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.
o It can have final methods which will force the subclass not to change the body of the
method.

Example of abstract class

1. abstract class A{}

Abstract Method in Java


A method which is declared as abstract and does not have implementation is known
as an abstract method.

Example of abstract method

1. abstract void printStatus();//no method body and abstract

Example of Abstract class that has an abstract method

In this example, Bike is an abstract class that contains only one abstract method run.
Its implementation is provided by the Honda class.

1. abstract class Bike{


2. abstract void run();
3. }
4. class Honda4 extends Bike{
5. void run(){System.out.println("running safely");}
6. public static void main(String args[]){
7. Bike obj = new Honda4();
8. obj.run();
9. }
10. }
running safely

Understanding the real scenario of Abstract class


In this example, Shape is the abstract class, and its implementation is provided by the
Rectangle and Circle classes.

Mostly, we don't know about the implementation class (which is hidden to the end
user), and an object of the implementation class is provided by the factory method.

• A factory method is a method that returns the instance of the class.

In this example, if you create the instance of Rectangle class, draw() method of
Rectangle class will be invoked.

File: TestAbstraction1.java

1. abstract class Shape{


2. abstract void draw();
3. }
4. //In real scenario, implementation is provided by others i.e. unknown by end user
5. class Rectangle extends Shape{
6. void draw(){System.out.println("drawing rectangle");}
7. }
8. class Circle1 extends Shape{
9. void draw(){System.out.println("drawing circle");}
10. }
11. //In real scenario, method is called by programmer or user
12. class TestAbstraction1{
13. public static void main(String args[]){
14. Shape s=new Circle1();//In a real scenario, object is provided through method, e.g., getShap
e() method
15. s.draw();
16. }
17. }
drawing circle
Abstract class having constructor, data member
and methods
An abstract class can have a data member, abstract method, method body (non-
abstract method), constructor, and even main() method.

1. //Example of an abstract class that has abstract and non-abstract methods


2. abstract class Bike{
3. Bike(){System.out.println("bike is created");}
4. abstract void run();
5. void changeGear(){System.out.println("gear changed");}
6. }
7. //Creating a Child class which inherits Abstract class
8. class Honda extends Bike{
9. void run(){System.out.println("running safely..");}
10. }
11. //Creating a Test class which calls abstract and non-abstract methods
12. class TestAbstraction2{
13. public static void main(String args[]){
14. Bike obj = new Honda();
15. obj.run();
16. obj.changeGear();
17. }
18. }
bike is created
running safely..
gear changed

Rule: If there is an abstract method in a class, that class must be abstract.


1. class Bike12{
2. abstract void run();
3. }
compile time error
Rule: If you are extending an abstract class that has an abstract method, you must
either provide the implementation of the method or make this class abstract.
Method Overriding in Java
• If subclass (child class) has the same method as declared in the parent class, it
is known as method overriding in Java.
• If a subclass provides the specific implementation of the method that has been
declared by one of its parent class, it is known as method overriding.

Usage of Java Method Overriding


o Method overriding is used to provide the specific implementation of a method which
is already provided by its superclass.
o Method overriding is used for runtime polymorphism

Rules for Java Method Overriding


1. The method must have the same name as in the parent class
2. The method must have the same parameter as in the parent class.
3. There must be an IS-A relationship (inheritance).

Example of method overriding

In this example, we have defined the run method in the subclass as defined in the
parent class but it has some specific implementation. The name and parameter of the
method are the same, and there is IS-A relationship between the classes, so there is
method overriding.

1. //Java Program to illustrate the use of Java Method Overriding


2. //Creating a parent class.
3. class Vehicle{
4. //defining a method
5. void run()
6. {System.out.println("Vehicle is running");}
7. }
8. //Creating a child class
9. class Bike2 extends Vehicle{
10. //defining the same method as in the parent class
11. void run()
12. {System.out.println("Bike is running safely");}
13.
14. public static void main(String args[]){
15. Bike2 obj = new Bike2();//creating object
16. obj.run();//calling method
17. }
18. }

Output:

Bike is running safely

Can we override static method?


No, a static method cannot be overridden. It can be proved by runtime polymorphism.

Why can we not override static method?


It is because the static method is bound with class whereas instance method is bound
with an object. Static belongs to the class area, and an instance belongs to the heap
area.

Can we override java main method?


No, because the main is a static method.
Final Keyword In Java
The final keyword in java is used to restrict the user.

The java final keyword can be used in many context. Final can be:

1. variable

2. method

3. class

1) Java final variable

If you make any variable as final, you cannot change the value of final variable(It will be
constant).

Example of final variable

There is a final variable speedlimit, we are going to change the value of this variable, but It can't
be changed because final variable once assigned a value can never be changed.

class Bike9{

final int speedlimit=90;//final variable

void run(){

speedlimit=400;

public static void main(String args[]){

Bike9 obj=new Bike9();

obj.run();

}//end of class

Output:Compile Time Error

2) Java final method


If you make any method as final, you cannot override it.

Example of final method

class Bike{

final void run(){System.out.println("running");}


}

class Honda extends Bike{

void run(){System.out.println("running safely with 100kmph");}

public static void main(String args[]){

Honda honda= new Honda();

honda.run();

Output:Compile Time Error

3) Java final class


If you make any class as final, you cannot extend it.

Example of final class

final class Bike{}

class Honda1 extends Bike{

void run(){System.out.println("running safely with 100kmph");}

public static void main(String args[]){

Honda1 honda= new Honda1();

honda.run();

Output:Compile Time Error

You might also like