CREATE OR REPLACE TYPE address_ty as object(
street VARCHAR2(20),
no VARCHAR2(10),
city VARCHAR2(20)
)INSTANTIABLE  FINAL;

CREATE OR REPLACE TYPE person_ty as object(
name varchar2(20),
surname varchar2(20),
address address_TY,
MEMBER FUNCTION to_string RETURN VARCHAR2
) INSTANTIABLE  NOT FINAL;

CREATE or replace TYPE BODY person_ty is
member function to_string RETURN VARCHAR2 is
  BEGIN
    RETURN '['||self.name||']['||self.surname||']';
  END to_string;
END;
/


CREATE OR REPLACE TYPE university_ty as object (
name varchar2(30)
);

CREATE OR REPLACE TYPE student_ty under person_ty (
enrllmentnumber number(5),
university ref university_ty ,
overriding MEMBER FUNCTION to_string RETURN VARCHAR2
) INSTANTIABLE  NOT FINAL


CREATE OR REPLACE TYPE BODY student_ty is
overriding member function to_string RETURN VARCHAR2 is
  BEGIN
    RETURN '['||(self as person_ty).to_string||self.enrllmentnumber||']';
  END to_string;
END;
/

create table persons of person_ty;
create table university of university_ty;

insert into university values (university_ty('University of Bari'));

insert into persons values (person_ty('Mario', 'Rossi', address_ty('via quercia','20','Bari')));
insert into persons values (
(select student_ty('Franco', 'Alti',555,ref(u)) from university u where name like '%Bari%'));

select p from persons p;
select value(p) from persons p;


select p.to_string() from persons p;
