Abstract classes
One of the fundamental concepts in OOP is the abstract class. Abstract classes cannot be instantiated and are designed to be subclassed. They are used to provide some common functionality across a set of related classes while also allowing default method implementations.
Rules to Remember
- Abstract classes cannot be instantiated.
- If a class has at least one abstract method, then the class must be declared abstract.
- To use an abstract class, we must create a class that extends the abstract class (inheritance) and provide implementations for all abstract methods.
- Java does not support multiple inheritance so we are only allowed to extend one class (abstract or not). There is where interfaces become useful.
package com.company.bean;
public abstract class Car {
public abstract void start();
public abstract void stop();
}
package com.company.bean;
public abstract class BMW extends Car{
@Override
public void start(){
System.out.println("BMW start");
}
}package com.company.bean;
public class Mercedes extends BMW{
@Override
public void stop() {
System.out.println("Mercedes stop");
}
}package com.company;
import com.company.bean.Car;
import com.company.bean.Mercedes;
public class Main {
public static void main(String[] args) {
Car c = new Mercedes();
c.start();
c.stop();
}
}Outcome will be:
BMW start
Mercedes stop
*Another interesting fact: Abstract class cann implement Interface. After implementing
we can override methods or not. But if Abstract class does not overrirde methods this means
override must be done by classes who extends Abstract classes.
package com.company.bean;
public interface Details {
public String name();
public double price();
}package com.company.bean;
public abstract class Car implements Details {
public abstract void start();
public abstract void stop();
@Override
public String name(){
return "Car implements method name in interface Detail";
}
@Override
public double price(){
return 100;
}
}package com.company.bean;
public abstract class BMW extends Car{
@Override
public void start(){
System.out.println("BMW start");
}
}package com.company.bean;
public class Mercedes extends BMW{
@Override
public void stop() {
System.out.println("Mercedes stop");
}
}package com.company;
import com.company.bean.Car;
import com.company.bean.Mercedes;
public class Main {
public static void main(String[] args) {
Car c = new Mercedes();
c.start();
c.stop();
System.out.println(c.name());
System.out.println(c.price());
}
}Outcome is:
BMw start
Mercedes stop
Car implements method name in Interface Detail
100.0
Lets imagine that class Car doen't override methods, then that means either class BMW or class
Mercedes has to override these two methods.
*Remember we can develop concrete(normal) methods inside abstract classes. Example:
package com.company.bean;package com.company.bean;
public abstract class Car {
public abstract void start();
public abstract void stop();
public void startAndStop() {
start();
stop();
}
public void speed(){
System.out.println("Speed");
}
}package com.company.bean;
public class Volvo extends Car {
@Override
public void start() {
System.out.println("Volvo starts");
}
@Override
public void stop() {
System.out.println("Volvo stops");
}
}package com.company;
import com.company.bean.Kia;
public class Main {
public static void main(String[] args) {
Kia k = new Kia();
k.startAndStop();
k.speed();
}
}Outcome:
Start and stop
Speed
*Object of an Abstract class impossible to create, even though it contains concrete methods.
It is possible if we override its methods.
package com.company.bean;
public abstract class Car {
public void speed(){
System.out.println("Speed");
}
}package com.company;
import com.company.bean.Car;
public class Main {
public static void main(String[] args) {
Car c = new Car() {
@Override
public void speed() {
super.speed();
}
};
c.speed();
}
}
Комментарии
Отправить комментарий