Go Back

Java example of Abstract class and abstract methods and declared abstract

7/25/2021
All Articles

#declared_abstract #abstract_keyword #abstract_methods  #Abstract #class #java ,

Java  example of  Abstract class   and abstract methods and declared abstract

Java  example of  Abstract class   and abstract methods 

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

below is key point to remaimber when you creating abstract class.

  • An abstract class is a class that is declared with abstract keyword.
  • An abstract method is a method that is declared without implementation.
  • An abstract class may or may not have all abstract methods. Some of them can be concrete methods.
  • A method defined abstract must always be redefined in the subclass, thus making overriding compulsory OR either make subclass itself abstract.
  • Any class that contains one or more abstract methods must also be declared with abstract keyword.
  • There can be no object of an abstract class. That is, an abstract class can not be directly instantiated with the new operator.
  • An abstract class can have parameterized constructors and default constructor is always present in an abstract class.

For More details visit official Documentation

you declare an abstract class, Car, to provide member variables and methods that are wholly shared by all subclasses:

 


package com.developer;
abstract class Car
{
int reg_no;
Car(int reg)
	{reg_no=reg;
	}
abstract void price(int p);
		
abstract void braking(int force);
}


Each nonabstract subclass of Car, such as braking and price, must provide implementations for the price and braking methods:


package com.developer;
class Maruti extends Car
{
Maruti(int reg_no)
	{
	super(reg_no);
	}
void price(int p)
	{
	 System.out.println("this show the price of maruti car");
	}	
void braking(int force)	
	{
	System.out.println("the maruti cars use hydraulic brakes");
	}

public static void main(String args[])
{
Maruti m = new Maruti(10);
m.price(7);
m.braking(9);
}	
	
}


Conclusion

Here we learn  how to implement Abstract class in Java .

with example of Car and Maruti as sub class.

Hope you like it subscribe our instagram and facebook account for more update .

Article