Lecture2 Intro Java Part2
Lecture2 Intro Java Part2
Methods
Lecture 2 -(Intro Java)
Code reusing
Creation of new classes using the existing classes
- Composing: The new class consists of instance objects of the existing classes
- Inheritance: A new class is created by extending an existing class (new fields
and methods are added to the fields and methods of the existing class)
Composing
The new class contains fields which are instance objects of the
existing classes.
class Adresa{
private String nr, strada, localitate, tara;
private long codPostal;
//...
}
class Persoana{
private String nume;
private Adresa adresa;
private String cnp;
//...
}
class Scrisoare{
private String
private Adresa
private String
private Adresa
//...
}
destinatar;
adresaDestinatar;
expeditor;
adresaExpeditor
Inheritance
NewClass
ExistingClass
Using inheritance, NewClass will have all the members of the class
ExistingClass. However, NewClass may either redefine some of the methods
of the class ExistingClass or add new members and methods.
UML notation:
Inheritance
public class Punct{
private int x,y;
public Punct(int x, int y){
this.x=x;
this.y=y;
}
public void muta(int dx, int dy){
//...
}
public void deseneaza(){
//...
}
}
public class PunctColorat extends Punct{
private String culoare;
public PunctColorat(int x, int y, String culoare){...}
public void deseneaza(){...}
public String culoare(){...}
}
Inheritance
Notions
Heap memory:
Punct punct =new Punct(2,3);
PunctColorat punctColorat=new PunctColorat(2,3,alb);
Method overloading
public class A{
public void f(int h){ //...
}
public void f(int i, char c){ //...
}
}
B b=new B();
b.f(23);
b.f(2, c);
b.f(mere,5);
Protected
The fields and methods which are declared protected are visible inside the
class, inside the derived classes and inside the same package.
Protected
public class Persoana{
protected String nume;
protected int varsta;
public Persoana(String nume, int varsta){
this.nume=nume;
this.varsta=varsta;
}
//...
}
public class Angajat extends Persoana{
protected String departament;
public Angajat(String nume, int varsta, String departament){
this.nume=nume;
this.varsta=varsta;
this.departament=departament;
}
//...
}
Fields initialization
Static fields.
2.
3.
The other fields are intialized with their types default values.
4.
Fields initialization
public class Produs{
static int contor=0;
//(1)
private String denumire;
//(2)
private int id=contor++;
//(3)
public Produs(String denumire){ //(4)
this.denumire=denumire;
}
public Produs(){
//(5)
denumire=;
}
//...
}
Produs prod=new Produs();
First the base class fields are initialized and then those of the derived class
In order to initialize the base class fields the default base class constructor is called by
default. If the base class does not have a default constructor, each constructor of the
derived class must call explicitly one of the base class constructors.
public class A{
protected int ac=3;
//...
}
To call the overridden method (from the base class) from the overriding method (from
the subclass).
Method overriding
Rules:
1.
The class B overrides the method mR of the class A if mR is defined in the class B
with the same signature as in the class A.
2.
//method mR from A
a=new B();
a.mR();
3.
//method mR from B
The methods which are not overridden are called based on the variable type.
Method overriding
4.
public class A{
public void mR(){
//...
}
}
4.
The return type of an overriding method may be a subtype of the return type of
the overridden method from the base class (covariant return type). ( JSE>=5).
public class C{
public SuperA m(){...}
}
public class D extends C{
public SubB m(){...}
}
toString()
equals()
//false;
//true, Punct must redefine equals
//toString is called
Polymorphism
Polymorphism
Polymorphic collections
public FiguraGeometrica[] genereaza(int dim){
FiguraGeometrica[] fg=new FiguraGeometrica[dim];
Random rand = new Random(47);
for(int i=0;i<dim;i++){
switch(rand.nextInt(3)) {
case 0: fg[i]= new Cerc(); break;
case 1: fg[i]= new Patrat(); break;
case 2: fg[i]= new Triunghi(); break;
default:
}
}
return fg;
}
public void muta(FiguraGeometrica[] fg){
for(FiguraGeometrica f: fg)
f.muta(3,3);
}
Abstract classes
An abstract method is declared but not defined. It is delared with the keyword
abstract.
Abstract classes
An abstract class cannot be instantiated.
1.
If a class contains at least one abstract method then that class must be
abstract.
A class can be declared abstract without having any abstract method.
If a class extends an abstract class and does not define all the abstract
methods then that class must also be declared abstract.
2.
3.
4.
Java interfaces
interface.
Interface implementation
Extending an interface
Multiple inheritance.
public interface A{
int f();
}
public interface B{
double h(int i);
}
public interface C extends A, B{
boolean g(String s);
}
Collisions
interface I1 {
void f();
}
interface I2 {
int f(int i);
}
interface I3 {
int f();
}
class C {
public int f() {
return 1;
}
}
interface I6 extends I1, I2{}
interface I4 extends I1, I3 {}
//error
The class must implement the methods from all interfaces.It may occur
collisions between methods declared in different interfaces
A class can inherit one class but can implement multiple interfaces
Have fields
Have constructors
No constructors.