Java abstraction class and programming example
Ø Inheritance 1. IS-A relationship 2. (HAS-A relationship) Ø Polymorphism 1. Method overloading 2. Method overriding |
|
1. Abstraction 2. Data hiding 3. Encapsulation 4. Tightly coupled class
|
Abstract class:-
abstraction is a hiding internal implementation
and just highlighting the setup that we are offering.
Q.1 don’t get confused between abstraction and encapsulation?
Abstraction |
Encapsulation |
Abstraction is a detail
hiding(implementation hiding) |
Encapsulation is a data hiding (information hiding) |
Data abstraction deals with exposing
the interface to the user and hiding details of implementation. |
Encapsulation groups together data methods that act upon the data. |
Abstraction is two there are
described below:-
1.
Using abstract
class (0 to 100% security)
2.
Using
interface class (100% security)
Ø A method without the body (no implementation ) is known
as an abstract method.
Ø A method must always be declared in an abstraction
class. Or We can say that if a class has
an abstract method, it should be declared abstract as well.
Ø If a regular class extends an abstraction class .when
the class must have to implement all the abstract methods of the abstract
parent class or it has to be declared abstract as well.
Ø Abstract methods in an abstract class are meant to be
overridden in derived concrete classes otherwise a compile-time error will be thrown.
Ø Abstraction classes cannot be instantiated, means we
can’t create an object of abstract class.
Example:-
abstract class Vehicle {
abstract void start(); {
class Car extends
Vehicle {
void start() {
System.out.println(“car start with key”);
}
}
class Scoter extends Vehicle
{
void start() {
System.out.println(“car start with
key”);
}
public static void
main(String args[]) {
Car c= new Car();
c.start();
Scoter s= new Scoter ();
s.start();
}
}
0 Comments