Users and WebSites

Report a typo

You are writing an application which collects information about what sites were visited by what users. There are three classes: User, WebSite and Visit. Many fields and methods of these classes are the same.

Write a new base abstract class named BaseEntity. Provided classes must extend it. Move all repeating fields and methods to the new class.

After your modifications, the following code must work correctly:

User user = new User();
user.setName("John Grant");

BaseEntity userEntity = user;
userEntity.setId(100);
userEntity.setVersion(1);

WebSite site = new WebSite();
site.setUrl("https://hyperskill.org");
        
BaseEntity siteEntity = site;
siteEntity.setId(101);
siteEntity.setVersion(1);
        
Visit visit = new Visit();
visit.setUser(user);
visit.setSite(site);
        
BaseEntity baseVisit = visit;
baseVisit.setId(102);
baseVisit.setVersion(103);
Write a program in Java 17
class User {

protected long id;

protected long version;

protected String name;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public long getVersion() {
return version;
}

public void setVersion(long version) {
this.version = version;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

class Visit {

protected long id;
___

Create a free account to access the full topic