0% found this document useful (0 votes)
10 views32 pages

Lecture01 Intrduction To Java Part1

The document provides an overview of Object-Oriented Programming (OOP) principles including abstraction, encapsulation, inheritance, and polymorphism, specifically in the context of Java. It covers class structure, methods, constructors, and the process of designing object-oriented programs. Additionally, it includes practical examples and guidelines for coding in Java, such as variable declaration and method calling.

Uploaded by

nmnq2015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views32 pages

Lecture01 Intrduction To Java Part1

The document provides an overview of Object-Oriented Programming (OOP) principles including abstraction, encapsulation, inheritance, and polymorphism, specifically in the context of Java. It covers class structure, methods, constructors, and the process of designing object-oriented programs. Additionally, it includes practical examples and guidelines for coding in Java, such as variable declaration and method calling.

Uploaded by

nmnq2015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

1

JAVA REVISITED
CSC 212: Data Structures
2

Objective
• Object Oriented Programming (OOP): What, Why,
How
• Analyzing and Designing OO Programs (Objects
& Classes)
• Java Syntax, Java Program Skeleton
• Analyzing and Designing a Program
• Preparing Classes.
3

Object-Oriented Design Principles


• Abstraction
• To distill a complicated system down to
its most fundamental parts and describe
these parts in a simple, precise
language.

• Encapsulation
• Different components of a software
system should not reveal the internal
details of their respective
implementations.
4

Object-Oriented Design Principles


• Inheritance
• Properties of a data type can be passed down to
a subtype. We can build new types from old ones
• We can build class hierarchies with many levels
of inheritance
• Allows the design of general classes that can be
specialized to more particular classes, with the
specialized classes reusing the code from the
general class.
5

Object-Oriented Design Principles


• Polymorphism
• Polymorphism is the capability of a method to do
different things based on the object that it is acting
upon.
• Following concepts demonstrate different types
of polymorphism in java.
• Method Overloading
• Method Overriding
6

Object-Oriented Design Principles


• Overloading :
• Allows the same method name to have multiple
meanings or uses.
• Overloading of methods allows to use the same name
to perform different actions depending on
parameters.
• Overloaded methods are differentiated only on the
number, type and order of parameters, not on the
return type of the method.
• Overriding:
• Declaring a method in subclass which is already
present in parent class is known as method overriding.
7

Inheritance

Person

Employee Teacher

Payment Contract
8

Polymorphism
• The derived classes
override the definitions Employee
to provide unique
behavior for each
specific type of
Employee
Payment Contract

• Dynamic binding : calcSalary () calcSalary ()


• Which method to call?
• Binding occurs at run
time, based on the type of
object.
9

Designing Object Oriented


• It’s a process of planning a computer program where
objects will interact with each other to solve specific
problems.

• Example :

A hotel contains a number of Residential rooms, Meeting


rooms, Wedding halls where the customer can reserve
any of them by choosing type of room and specify date
and time. You are required to design reservation system
for Hotel. where it can offer the available rooms or halls
and allows the customer to reserve one of them.
10

Designing Object Oriented


• Objects = nouns
Functions to be encapsulated

customer reservation room

regular meeting hall


11

Class
• Class: a unit of abstraction in an object
oriented (OO) program
• Represents similar objects
• Its instances
• A kind of software module: Describes its
instances’ structure (properties). Contains
methods to implement their behavior.
12

Class Structure

• Two Main Sections


◦ Variables: can be a simple data type or
another class
• Represent the State of the Class
• Define Data represented in an Class
• Associations

◦ Operations :
• A procedural abstraction used to implement the
behavior of a class.
13

Skelton of Class
class ClassName {
// Attributes
Type Name;
//Constructor
public ClassName (Type n,…) {
}
//Setter
public void setName(Type n)
{
}
//Getter
public Type getName()
{
}
//Operations
}
14

Java Rules
• The name of a class is the same as
the name of the file (which has .java
extension)
• All Java applications must have a
main method in one of the classes
• Execution starts in the main method
• Body of a method is within { }
• All other statements end with
semicolon ;
15

Variables and data type


• String name=“Ali”;
• name is a variable of type String
• we have to declare variables before we use them
• Variables can be declared anywhere within a block
• Variable name
• use meaningful names numberOfBricks
• start with lower case
• capitalize first letter of subsequent words
• can not start with digit
• can not use keyword
16

Data types
• int 4 byte integer (whole number)
◦ range -2147483648 to +2147483648
• float 4 byte floating point number
◦ decimal points, numbers outside range of int
• double 8 byte floating point number
◦ 15 decimal digits (float has 7) so bigger precision
and range
• char 2 byte letter
• String string of letters
• boolean true or false (not 1 or 0)
17

Output
• Java provides print methods in the class
System.out (don’t need to import)
• println(name);
◦ prints out what is stored in name, then goes to a
new line
• print(name);
◦ prints out what is stored in name, but does not
start a new line
• print("My name is " + name);
◦ put text in quotes
◦ use + to print more than one item
18

Methods
• methods break down large problems into
smaller ones
• your program may call the same method
many times
• ◦ saves writing and maintaining same code
• methods take parameters
• ◦ information needed to do their job
• methods can return a value
• ◦ must specify type of value returned
19

Methods
Method Header :
visibility [static] returnType
methodName(parameterList)
• visibility:
• public
accessible to other classes
• protected
accessible to classes which inherit from this one
• private

• static keyword:
• use when method belongs to class as whole not object of the
class
20

Methods
• returnType:
• type of the value that the method calculates and returns (using
return statement)
• can return object
• if nothing returned, use keyword void

• methodName: Java identifier; use meaningful name which


describes what method does!
• formal parameter list:
• information needed by method
• pairs of type name
• examples:
• addNums(int num1, int num2)
• drawPerson(boolean isBald, String name, int
• use empty brackets if method has no parameters
printHeadings()
21

Method Body
• use curly brackets to enclose method body
all your code goes in here
• it should return a value of appropriate type
• ◦ must match type in method header
◦ nothing is executed after return
statement
◦ if method returns void, can omit return
statement
• method will automatically return at closing }
22

Calling Method
• methods will not run unless called from
elsewhere
• ◦ a statement in main() method could call another
method
• ◦ this method could call a third method .....
•  class methods are called with the form:

• ClassName.methodName(parameters);
• ◦ omit ClassName if called in same class
•  method name and parameters must match the

•  if the method returns a value, it can be stored

in a variable or passed to another method


23

Passing parameters
public class Test{
public static void main(String args[]) {
int a =5;
System.out.println("Before Modify: " + a);
modify(a);
System.out.println("After Modify: " + a);
}
public static void modify(int x)
{
x++;
}
}
24

Passing parameters
class Student { public class Test{
String name; public static void main(String args[]) {
int id; Student a = new Student (”Ahmed");
System.out.println("Before Modify: " + a);
public Student(String name) {
modify(a, “Ahmed Alli");
this.name = name;}
System.out.println("After Modify: " + a);
public String toString() { }
return name;} public static void modify(Student s,String n)
public void setName(String name) {
{ s.setName(n);
this.name = name;} }
} }
25

Constructors
• A constructor is a special kind of method that
is used to initialize newly created objects.
• Java has a special way to declare the
constructor and a special way to invoke the
constructor.
• A class may contain more than one constructor.
• Constructor has the same name as the class
name.
• invoked “automatically” at the time of object
creation.
26

Object
• a new object is created from a defined class by
using the new operator.
• The new operator creates a new object from a
specified class and returns a reference to that
object.
• Example
Student S1=new Student (“Ahmed ”,29211212);
• In order to access attributes of a given object:
• use the dot (.) operator with the object reference (instance variable) to
have access to attributes’ values of a specific object.
• ObjectName.VariableName
• ObjectName.MethodName(parameter-list)


27

Casting
• Taking an Object of one particular type and “turning it into”
another Object type.
• Primitive Type Casting:
public class MainClass{
public static void main(String[] argv){
int a = 100;
float f=100.00f;
long b = a; // Implicit cast, an int value always fits in a
long
int c = (int)f; // Explicit cast, the float could lose inf
}
}
28

Casting
Object Type Casting: Car c = new Car();
interface Vehicle { Ford f = new Ford();
}
c=f;
class Car implements
Vehicle {
void carMethod() c.carMethod();
{}
} //How can I access fordMethod ()
class Ford extends Car { if (c instanceof Ford)
void fordMethod () {} ((Ford) c).fordMethod ();
}
29

Extending Class
• Inheritance in Java is implemented by
extending a class
public class subClass extends superClass {

• We then continue the definition of subClass as


normal
• However, implicitly subClass contains all the data
and operations associated with superClass. Even
though we don’t see them in the definition
30

Interfaces
• Interface is a collection of method declarations with
no bodies.
public interface NameOfInterface
{
//Any number of final, static fields
//Any number of abstract method declarations\
}
• Methods of an interface are always empty (that is,
they are simply method signatures).

• When a class implements an interface, it must


implement all of the methods declared in the
interface.
31

Example
public interface Shape
{
public double calculate_Area();
}
public class Rectangle implements Shape
{
double w,h;
public double calculate_Area()
{
return w*h;
}
}
32

To Do
•  Read Chapter 1 & 2 of the Textbook.
•  Install eclipse or any java editor you fancy.

You might also like