Java Specific Elements: Course Software Engineering in Electronics and Telecommunications English Classes
Java Specific Elements: Course Software Engineering in Electronics and Telecommunications English Classes
Java Specific Elements: Course Software Engineering in Electronics and Telecommunications English Classes
Overview
Interfaces in Java
Packages in Java
Inheritance in Java
Abstract classes and Interfaces in Java
Java Interfaces
As an abstract class defined by:
interface Interfata1{
//declarare de constanta statica
public static final int max = 200;
//declarare de metoda
public void metoda();
}
implementata);
class Test{
interfetei
C1 obiect_clasa = new C1(); //instantierea clasei
obiect_interfata = obiect_clasa;//upcasting
//apelul metodei declarate in interfata si implementate in clasa
obiect_interfata.metoda();
}
}
Java 8 interfaces
public interface Addressable
{
String getStreet();
String getCity();
interface X
{
System.out.println("foo");
}
}
class Y implements X
{
}
public class Z
{
X.foo();
}
}
Expression Y.foo() will not compile because foo() is a static member of
interface X and not a static member of class Y.
Packages in Java
Definition: A collection of classes and
interfaces grouped with the same name as
an association. Are simple defined with:
package PackageName; //used with:
import PackageName.*;//or a CName for *
Steps to Creating a
Program in Eclipse
To create a program in Eclipse:
1. Create a Project
2. Create a Package (inside the src folder
of the project)
3. Create a Class (inside the package)
4. Create and run Java code
10
Package
11
12
13
14
package de.vogella.eclipse.ide.first;
}
You could also directly create new packages via this
dialog. If you enter a new package in this dialog, it is
created automatically.
16
class C1{//...
class C2{//...
interface I1{//...
}
import pack1.C1;
or
import pack1.*;
17
Multilevel packages
A hierarchy of packages
package pkg1[.pkg2[.pkg3]]
import pkg1[.pkg2].(className|*);
a)import java.util.*;
class MyDate extends Date{}
b) fully qualified name
class MyDate extends java.util.Date{}
18
Workbench Terminology
multi-level packages
Menu bar
Text
editor
Tool bar
Perspective
and
Fast View
bar
Outline
view
Resource
Navigator
view
Bookmarks
view
Properties
view
Message
area
19
Stacked
views
Tasks
view
Editor
Status
area
package Calin;
import java.io.*;
import java.util.*;
public class Citeste{
String citit;
public String CitesteTastatura(){
try
{
DataInputStream data=new DataInputStream(System.in);
citit=data.readLine();
}
catch(IOException ww)
{
System.out.print("A aparut o exceptie in pachetul propriu!!! ");
}
return citit;
}//end_met
}//class Citeste
20
Refactoring
Refactoring is the process of restructuring the code
without changing its behavior. For example renaming a
Java class method or package is a refactoring activity.
For example to use the Rename refactoring, you can
right-click on your class (in the editor or Package
Explorer) and select Refactor Rename to rename
your class. Eclipse will make sure that all calls in your
Workspace to your class or method are renamed.
For a Project click on the arrow to expand your project,
for a package right click on the package, and select
Refactor then Rename, make sure that update
references is checked.
22
Inheritance in Java
Similar with C++ but with some
differences.
Object class is the first class upper in the
root
General form:
[lista_modificatori] class idClasa [extends
Subclass
The more general class from which The more specific class
that
other classes derive their methods derives or inherits from another
and data.
class (the superclass).
25
Access modifiers:
public
private
protected subclass access in inheritance
process, but in Java is considered friendly
- private protected package private,
access in the same package by any class
(is not an explicit keyword!!!)
default no key word till Java8- able to
be accessed in the class and same
package by all classes.
27
Other Modifiers
final
For a variable: cannot be changed after instantiation
Widely used to make immutable classes
For a class: cannot be subclassed
For a method: cannot be overridden in subclasses
synchronized
Sets a lock on a section of code or method
Only one thread can access the code at any given time
volatile
Guarantees other threads see changes to variable
transient
Variables are not stored in serialized objects
native
Indicates that the method is implement using C or C++
}
}//Base class
public class Cat extends Animal {
@Override
}
}//Derived class
Enums in Java
Java enums are classes with named
instances
They do not correspond to ints as in C++
Quick example
public enum Month { JANUARY, , DECEMBER
}
doSomethingWith(Month.JANUARY);
Capabilities
Enums can have methods and constructors,
just like normal classes. Can be referenced in
switch statements.
32
Upcasting/downcasting
Casting:
Is the principle of changing the object
type during assignment.
Upcasting (from derived to base class):
loses access to specialized methods from
the sub-classed object instance.
Downcasting (from base to derived
class but with references): gains access to
specialized methods of the subclass.
33
Upcasting/downcasting
examples
class Bicicleta{
//
}//Base class
class BicicletaDeTeren extends Bicicleta{
//...
}//Derived class
Bicicleta b0 = new Bicicleta();
BicicletaDeTeren b1 = new BicicletaDeTeren();
34
Upcasting
b0 = b1; or
Bicicleta b2 = new BicicletaDeTeren();// or
Bicicleta b2 = (Bicicleta )new BicicletaDeTeren();
35
36
Downcasting
A reference to a derived class may be
associated in explicit mode with cast
operator to a reference to the base class
that is associated to an object of derived
class.
37
38
package updown;
// Import
import java.lang.String;
// Clasa de baza
class Feline
{
boolean Foame = false;
void Vorbeste()
{
System.out.println("\n Raaa!!!- Base class");
}
void Cheama()
{
System.out.println("\nHai felina, felina...Base class");
if(Foame)
Vorbeste();
}
}//Feline
39
//downcasting
Pisica_de_Casa Pisica;
Pisica=new Pisica_de_Casa();
Feline rFeline;
rFeline=Pisica;
rFeline.Vorbeste();
Pisica_de_Casa rPisica;
rPisica=(Pisica_de_Casa)rFeline;
rPisica.Vorbeste();//down
} // end main
} // end UpCasting
42
Results up/downcasting
Miau !!!- Derived Pisica_de_Casa
Hai felina, felina...Base class
Roar!!!- Derived Puma
Hai felina, felina...Base class
43
ClassCastException
44
//clasa de baza - CB
class Baza{
public Baza(){
46
//clasa derivata
class Derivata{
public Derivata(){
47
}
}
48
//clasa derivata
super.metoda();
//apelul metodei originale din CB
49
class Test{
b0.metoda();
b1.metoda();
}
50
51
Interface inheritance
//prima interfata de baza
interface Int1{
interface Int2{
53
System.out.println("metoda 1");
System.out.println("metoda 2");
System.out.println("metoda 3");
}
54
class Test{
ob1.m1();
ob1.m2();
ob1.m3();
55
58
calcWeight(), getPlanetGravity(),
displayWeight().
59
Java 8: Interfaces
and Abstract Classes
61