0% found this document useful (0 votes)
4 views

Java Classes

A background on the classes in java

Uploaded by

tendaimatsikure0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Classes

A background on the classes in java

Uploaded by

tendaimatsikure0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

OBJECT ORIENTED

CONCEPTS
Classes
• A class is a template/blueprint from which objects may be
created and it Can have any number of instances (objects).
• An object contains state (data/ fields) and behavior/actions
(methods)
• A class defines the instance variables and methods required by a
particular object
• instance variables hold data defined using data type variable name pairs


Access levels
• private—The member is accessible only from the top-level class where
it is
declared.
• package-private—The member is accessible from any class in the
package where it is declared. Technically known as default access, this is
the access level you get if no access modifier is specified.
• protected—The member is accessible from subclasses of the class
where it is
declared nd from any class in the package where it is declared.
• public—The member is accessible from anywhere.
Methods
• The behavior of an object is defined by a set of methods
(functions), which may access or manipulate the state.
• Mutators- changes the state of a class
• Accessors- access the value in instance variables
• The syntax of a method:

• Access-type return-type method-name(){


Method statements
}
• E.g

public void display () {


System.out.println ("Author: " + author + "\n" +"Title: " + title + "\n"
+"Year: " + year + "\n");
}
Return types
The type of data returned by a method eg:
void – returns nothing
int – returns an integer value
Constructors
• a special method which initializes an object immediately upon creation.
• It has the exact same name as the class in which it resides.
• A constructor has no return type, not even void.
• The constructor is automatically called to initialize the object.
Example
• public class Book {
String author;
String title;
String year;
double price;
//constructor
Book (String author, String title, String year, double price) {
this.author = author;
this.title = title;
this.year = year;
}
other methods
}
Field initialization

• All data fields are set to zero, false or null.


• The data fields with initializers are set, in the order in which they appear
in the class definition.
• The constructor body is executed.
Parameter passing
• Parameters are the data items sent between methods
• All method parameters are passed by value (i.e. modifications to parameters of
primitive types are made on copies of the actual parameters)
• Eg
• Book (String author, String title, String year, double price)

To use the constructor:


Book myBook= new Book(“Shakespeare”, “hamlet”, “1900”, 2.50)

the data types of the variables should be the same for both the calling and the called
method
Inheritance
• Inheritance defines a relationship between classes.

• When class C2 inherits from (or extends) class C1, class C2 is called a
subclass or an extended class of C1.
• C1 is the superclass of C2.
• Inheritance: a mechanism for reusing the implementation and
extending the functionality of super classes.
• All public and protected members of the superclass are
accessible in the extended class
• The subclass inherits features from its superclass, and may add more features.
• A subclass is a specialization of its superclass
• Inheritance refers to a “is a “ relationship
Inheritance hierarchy
Association
• Defines a relationship between classes of objects that allows one object
instance to cause another to perform an action on its behalf.
• It is a weak relationship between unrelated classes
• It is a “using” relationship between two or more objects in which the objects
have their on life time and there is no owner
• The relationship can be one to one, one to many or many to many
• E.g teacher and student
Aggregation
• A specialised form of association between two or more objects in which each
object has its own life cycle but there exists an ownership as well
• A whole/ part, parent/ child relationship which may or may not denote
physical containment
• An essential property of an aggregation relationship is that the whole/or
parent can exist without the part/child and vice versa Eg department and
employee, lecturer and course
Composition
• A specialised form of aggregation in which the child is destroyed if the parent
is destroyed
• Implements a “has a” relationship in classes
• The life cycle of the part is controlled by the whole/ parent that owns it i.e
there is a strong association between the parent and child classes
• Eg building and room
Exercise
• Research in pairs on
• association,
• Aggregation
• composition
• How are these implemented in java
Method overloading
• Ability to have multiple methods with same name
• Overloading is achieved by using different types or number of
parameters
• Overloaded methods may have the same or different return types, but
they must differ in parameters
Method overriding
• Provides specific implementation of a method which is
already provided by the superclass
• When a class is inheriting a method from a superclass of its
own, then there is an option of overriding
the method provided it is not declared as final.
Polymorphism
• Polymorphism means "many forms", and it occurs when we
have many classes that are related to each other by
inheritance.
• With polymorphism, the reference type can be a superclass of
the
actual object type.
• Examples of polymorphism types include:
• Polymorphic Parameter Passing and Polymorphic return
types
Abstract classes
A class that can never be instantiated but they can be subclassed
The sub class provides the implementation of all the abstract methods in the
parent class
Public abstract class className{
//declare fields
//declare non abstract and abstract methods
}
• abstract class MotorBike {
float speed;
abstract void brake();
abstract float move(float speed);

• }

class SportsBike extends MotorBike {

// implementation of abstract method


public void brake() {
System.out.println("SportsBike Brake");
}
Interfaces
• An interface provides a mechanism for providing optional behavior in
classes
• A class may implement more than one interface
Encapsulation
• Make instance variable private and access them through public methods
• Provide getter and setter methods to access and modify the state of instance
variables.

You might also like