J.P J.P - 2 months ago 7
Java Question

Inheritance abstract constructor in java

I have one thing which is not clear for me.

I have this abstract class with constructor:

private String naziv;
private String opis;
private Date datumKreiranja;
private List<Lekcija> listaLekcija;

public LekcijaAbstract(String naziv,String opis, Date datumKreiranja) {
this.listaLekcija=new ArrayList<Lekcija>();


And second class with extends abstract class :

public Lekcija(String naziv, String opis, Date datumKreiranja) {
super(naziv, opis, datumKreiranja);

I was wonder does this second class have private List listaLekcija; which we have in abstract class but it is private?


I was wonder does this second class have prrivate List listaLekcija; which we have in abstact class but it is private?

The class doesn't, but instances of the class do, because instances of the second class are also instances of the first class, which has that private member. Only code in the abstract class can access the list, since it's private to that class.

Here's a simpler example which shows how code in the base class can access the private field, and code in the subclass cannot:

Live Copy

import java.util.List;
import java.util.LinkedList;

abstract class Base {
    private List<String> strings;

    public Base() {
        this.strings = new LinkedList<String>();

    protected void remember(String s) {

    public int rememberedCount() {
        return this.strings.size();

class Derived extends Base {
    public Derived() {
        // Would fail with error:
        // this.strings.add("three");

class Test {
    public static final void main(String[] args) {
        Derived d = new Derived();
        System.out.println(d.rememberedCount()); // 2

Note that:

  • Only code in Base can directly interact with strings

  • Code in Base can selectively expose things about strings to both subclasses (the remember method) and the world at large (the rememberedCount method)

(The above is purely for demonstrating concepts, not a suggestion of API or class design.)