SOFTWARE DESIGN TECHNIQUES (CSCN72040)
Week-3
Java Programming: Part-2
Jan 26th , 2024
Akrem El-ghazal
Review
Introduction
Java is a high-level object-oriented programming
language. It is related to C++, which is a direct
descendant of C. Much of the character of Java is
inherited from these two languages. From C, Java
derives its syntax. Many of Java’s object-oriented
features were influenced by C++. In fact, several of
Java’s defining characteristics come from—or are
responses to—its predecessors [1].
No surprise that you will see some
similarity between Java and C++
Schildt, Herbert. "Java: the complete reference." (2021).
HELLO JAVA PROGRAM
com
yourCompanyName
[Link]
*.JAVA VS *.CLASS
Java code (*.java)
Compiler
Bytecode (*.class)
JVM
JAVA PACKAGES
Packages are containers for classes. They are used to keep
the class name space compartmentalized. For example, a
package allows you to create a class named Map, which
you can store in your own package without concern that it
will collide with some other class named list stored elsewhere.
Packages are stored in a hierarchical manner and are
explicitly imported into new class definitions [2].
com
yourCompanyName
[Link]
HELLO JAVA PROGRAM
com
yourCompanyName
[Link]
package1
[Link]
Package [Link]
Provides classes that are fundamental to the design
of the Java programming language.
[Link]
PACKAGE [Link]
import [Link].*;
CLASSES AND OBJECTS IN JAVA
Syntax:
<access modifier> class class_name {
// Member variables
// Class methods
}
Getters and setters
ACCESS MODIFIERS
Access level modifiers determine whether other classes can use a
particular field or invoke a particular method [1]
package private
public protected default private
Within the same class Yes Yes Yes Yes
Within the same package Yes Yes Yes No
Different package Yes Yes No No
subclass
Different package Yes No No No
Non-subclass
CREATING AN OBJECT
You can create an object from a class by using the new operator and a constructor. The new operator returns a
reference to the object that was created.
public class Person {
int age;
public void display() { Heap memory
[Link]("I am a person");
}
} Stack age
0x2001 display()
x=5
int x = 5; p = 0x2001
Person p = new Person();
Is a reference to the object
Use dot (.) notation to access the class
members [Link]()
JAVA GARBAGE COLLECTION
public class Person {
int age;
public void display() { Garbage
collection
[Link]("I am a person");
}
Heap memory
}
Stack age
0x2001 display()
p = 0x2001
0x2022 age
Person p = new Person(); 0x2022 display()
p = new Person();
INHERITANCE
To inherit from class, use the extends keyword Student class inherits from Person class
Superclass
Person
Base class
Subclass
Student
Derived class
Override annotation
Student class
Person class
CLASS OBJECT
Class Object is the root of the class hierarchy. Every class has Object as a
superclass. All objects, including arrays, implement the methods of this class[1].
SOURCE: [Link]
Override the toString method
JAVA STRING
Strings are widely used in java programming. They are a sequence of
characters. In the java programming language, strings are objects [1].
Creating a string
using string literal
Heap memory
s1
“Java”
Creating a string s2
String constant pool
using new keyword
s3 “Java”
The String constant pool is a special memory area in
the heap. If you declare a String literal, the JVM
creates the object in the pool and stores its
reference on the stack.
Introduction
Static and final key words
Abstract classes and methods
Interfaces
OUTLINE Abstract class vs interface
Wrapper classes
Java Generics
Arrays, List and ArrayList
Summary and Conclusion
INTRODUCTION
STATIC KEYWORD
STATIC KEYWORD
The static keyword is used to create fields and methods that belong to the class,
rather than to an instance of the class [1].
FINAL KEYWORD
Final KEYWORD
The final keyword can be used with variables , method and classes
final primitive type variable Once you initialize it , you cannot modify it
final reference type variable Once you initialize it , you cannot refer it to another object
final method A final method cannot be overridden
final class A final class cannot be subclassed
ABSTRACT CLASS
ABSTRACT CLASS
An abstract class is a class that is declared abstract. The abstract
class may or may not include abstract methods. Abstract classes
cannot be instantiated, but they can be subclassed.
public abstract class Shape {
abstract void display(); Shape
An abstract method: Square
It is a method that is declared without an implementation; like
this:
abstract void display();
If an abstract class is subclassed, then the subclass usually provides implementations for all
the abstract methods in its parent class. However, if it does not, then the subclass must also
be declared abstract [1].
INTERFACE
INTERFACE
Implementing an interface allows a class to become more
formal about the behavior it promises to provide. Interfaces form
a contract between the class and the outside world, and this
contract is enforced at build time by the compiler. If your class
claims to implement an interface, all methods defined by that
interface must appear in its source code before the class will
successfully compile [1].
INTERFACE
An interface declaration consists of modifiers, the keyword interface,
the interface name, a comma-separated list of parent interfaces (if
any), and the interface body. For example: Movable
public interface Movable {
void move();
Square
}
The public access specifier indicates that the interface can be used by any class in any package. If you do not specify
that the interface is public, then your interface is accessible only to classes defined in the same package as the
interface.
An interface can extend other interfaces, just as a class subclass or extend another class. However, whereas a class can
extend only one other class, an interface can extend any number of interfaces. The interface declaration includes a
comma-separated list of all the interfaces that it extends[1].
The interface body can contain abstract methods, default methods, and static methods. An abstract method within an
interface is followed by a semicolon, but no braces (an abstract method does not contain an implementation).
Default methods: are defined with the default modifier, and static methods with the static keyword. All abstract, default,
and static methods in an interface are implicitly public, so you can omit the public modifier [1].
An interface can contain constant declarations. All constant values defined in an interface are implicitly public, static,
and final. Once again, you can omit these modifiers [1].
INTERFACE
An interface declaration consists of modifiers, the keyword interface,
the interface name, a comma-separated list of parent interfaces (if
any), and the interface body. For example:
public interface Movable {
Movable
static final int maxSpeed = 200;
void move();
}
Bmw
All fields are
static and
final
INTERFACE
Default Methods
Default methods enable you to add new functionality to the interfaces of your
libraries and ensure compatibility with code written for older versions of those
interfaces [1] .
ABSTRACT CLASS VS INTERFACE
ABSTRACT CLASS VS INTERFACE
Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of
methods declared with or without an implementation. However, with abstract classes, you can declare fields
that are not static and final, and define public, protected, and private concrete methods. With interfaces, all
fields are automatically public, static, and final, and all methods that you declare or define (as default
methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you
can implement any number of interfaces [1].
Which should you use, abstract classes or interfaces?
Consider using abstract classes if any of these statements apply to your situation[1]:
You can extend only
• You want to share code among several closely related classes.
one class, whereas
• You expect that classes that extend your abstract class have many common methods or fields, or require
you can implement
access modifiers other than public (such as protected and private).
any number of
• You want to declare non-static or non-final fields. This enables you to define methods that can access and
interfaces
modify the state of the object to which they belong.
Consider using interfaces if any of these statements apply to your situation [1]:
• You expect that unrelated classes would implement your interface. For example, the interfaces
Comparable is implemented by many unrelated classes.
• You want to take advantage of multiple inheritance of type.
WRAPPER CLASSES
WRAPPER CLASSES
Wrapper classes provide a way to use primitive data types as objects. The
following table lists the primitive types and their corresponding wrapper
classes:
Primitive type Wrapper class
boolean Boolean
byte Byte
char Character
• Data structures in the Collection framework such
as ArrayList works only with objects float Float
• Generic do not allow using primitive types int Integer
long Long
short Short
double Double
JAVA GENERICS
JAVA GENERICS
Mainly generics enable types (classes and interfaces) to be parameters when
defining classes, interfaces and methods. Type parameters provide a way for you
to re-use the same code with different inputs.
Generics do not
allow using
primitive types
Different types as parameters
ARRAYS, LIST AND ARRAYLIST
ARRAYS, LIST AND ARRAYLIST
Arrays
It is a container object that holds a fixed number of values of a single type. The length of an
array is established when the array is created. After creation, its length is fixed [1].
The List Interface
A List is an ordered Collection. Lists may contain duplicate elements. In addition to
the operations inherited from Collection, the List interface includes operations for
the following [1]: The Java platform
contains two
general-purpose List
Positional access — manipulates elements based on their numerical position in the
implementations.
list. This includes methods such as get, set, add, addAll, and remove. ArrayList and
Search — searches for a specified object in the list and returns its numerical LinkedList
position. Search methods include indexOf and lastIndexOf.
Iteration — extends Iterator semantics to take advantage of the list's sequential
nature. The listIterator methods provide this behavior.
Range-view — The sublist method performs arbitrary range operations on the list.
ArrayList
Resizable-array implementation of the List interface[1].
[Link]
SUMMARY
SUMMARY
Explained static and final
keywords
Explained Java Generics
To do
NEXT STEPS
Further Reading
FURTHER READING
Will be posted in the course shell
REFERENCES
1. Oracle Java Decumulation
2. Herbert Schildt Java: The Complete Reference12th Edition 2021
THANK YOU