concrete java_Concrete Class in Java

First, we"ll define the term. Then, we"ll see how it"s different from interfaces and abstract classes.

2. What Is a Concrete Class?

A concrete class is a class that we can create an instance of, using the new keyword.

In other words, it"s a full implementation of its blueprint. A concrete class is complete.

Imagine, for example, a Carclass:

1

2

3

4

5

6

7

8

9

public

class

Car {

public

String honk() {

return

"beep!"

;

}

public

String drive() {

return

"vroom"

;

}

}

Because all of its methods are implemented, we call it a concrete class, and we can instantiate it:

1

Car car =

new

Car();

Some examples of concrete classes from the JDK are HashMap, HashSet, ArrayList, and LinkedList.

3. Java Abstraction vs. Concrete Classes

Not all Java types implement all their methods, though. This flexibility, also called abstraction, allows us to think in more general terms about the domain we"re trying to model.

In Java, we can achieve abstraction using interfaces and abstract classes.

Let"s get a better look at concrete classes by comparing them to these others.

3.1. Interfaces

An interface is a blueprint for a class. Or, in other words, its a collection of unimplemented method signatures:

1

2

3

4

interface

Driveable {

void

honk();

void

drive();

}

Note that it uses the interface keyword instead of class.

Because Driveablehas unimplemented methods, we can"t instantiate it with the newkeyword.

But, concrete classes like Carcan implement these methods.

The JDK provides a number of interfaces like Map, List, and Set.

3.2. Abstract Classes

An abstract class is a class that has unimplemented methods, though it can actually have both:

1

2

3

4

5

6

7

public

abstract

class

Vehicle {

public

abstract

String honk();

public

String drive() {

return

"zoom"

;

}

}

Note that we mark abstract classes with the keyword abstract.

Again, since Vehiclehas an unimplemented method, honk, we won"t be able to use the newkeyword.

Some examples of abstract classes from the JDK are AbstractMap and AbstractList.

3.3. Concrete Classes

In contrast, concrete classes don"t have any unimplemented methods.Whether the implementations are inherited or not, so long as each method has an implementation, the class is concrete.

Concrete classes can be as simple as our Car example earlier. They can also implement interfaces and extend abstract classes:

1

2

3

4

5

public

class

FancyCar

extends

Vehicle

implements

Driveable {

public

String honk() {

return

"beep"

;

}

}

The FancyCarclass provides an implementation for honk and it inherits the implementation of drive from Vehicle.

As such, it has no unimplemented methods. Therefore, we can create a FancyCar class instance with the new keyword.

1

FancyCar car =

new

FancyCar();

Or, simply put, all classes which are not abstract, we can call concrete classes.

4. Summary

In this short tutorial, we learned about concrete classes and their specifications.

Additionally, we showed the differences between interfaces and concrete and abstract classes.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值