MIDTERM (1)

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 40

MIDTERM

CLASS AND OBJECT


• Class is a blueprint/template that are used to create objects.
This describes the behaviour/state that the object of its type
support.
• Object − Objects have states and behaviors... An object is an
instance of a class.Class.
• All classes are stored in
a folder namely package.
• To create a class its
header should have a key
word class.
VARIABLE TYPES • Class variables − Class variables
• Local variables − are variables declared within a
Variables defined inside class, outside any method, with the
methods, constructors static keyword.
or blocks are called local
variables.
• Instance variables −
Instance variables are
variables within a class
but outside any method.
These variables are
initialized when the class is
instantiated..
CREATING AN OBJECT
• Declaration − A variable declaration with a variable name
with an object type.
• Instantiation − The 'new' keyword is used to create the
object.
• Initialization − The 'new' keyword is followed by a call to
a constructor. This call initializes the new object.
CONSTRUCTOR
– initializes all the member
variables and operations to
prepare the class to be used
.
– No return type
– Name of constructor should
be the same as the class 1st Object
–Every class has a constructor. 2nd Object
– A class can have more than 3rd Object
one constructor
– If there is no explicit
declaration of constructor jvm
automatically creates one.
METHOD
Method definition in order: Public or private, void or
returning a value, method name, parameters.

Access Modifier – public, private and protected


Returning value – primitive data types and reference data
types
Method name – name of the method which is a series of
statement that carry out some task.
Parameters – It is the part of the called method that receives the
data and act as a place-holder for some value
METHOD SIGNATURE Method name can be the same as long as the parameter
METHOD INVOKING supplied and return type is different … this is also
called method overloading.
SETTER
(MUTATOR)
GETTER
( ACCESSOR)
Mutator Method(set) – method that is
used to control changes to a variable.

Accessor Method (get) –


A method that provides the means for the
state of an object to be accessed from
other parts of a program.

The “this” keyword is a reference of the


current object.
INHERITANCE

The mechanism in java by which one class is allow Advantage of Inheritance


to inherit the features(fields and methods) of another -Reusability of codes
class. -Automatic enhancement of the base
class will also applies in derived class.
This “extends” keyword indicates that a class inherits -Similar classes can be made to behave
from another class. consistently
-Reliability of code as base class code
The class whose features are inherited is known as will already be tested and debugged.
super class(or a base class or a parent class)
The class which inherits the properties of other is
known as subclass (derived class, child class,).
A subclass (for this example is “test”) inherit both
member variables and methods of the superclass (for
this example “calculation”) and in an inheritance
relationship this is the specialized class.
TYPES OF INHERITANCE

SINGLE MULTILEVEL HIERARCHICAL HYBRID

SUPER SUPER SUPER SUPER


CLASS CLASS CLASS CLASS

SUB CLASS SUB CLASS


1 2
SUB CLASS
1

SUB CLASS SUB CLASS SUB CLASS SUB CLASS SUB CLASS
1 1 2 3 3

SUB CLASS
2
SUPER AND SUB KEYWORD
TEST CLASS SUB CLASS SUP CLASS

Parent Class instance


super is used to refer immediate
variable
parent class instance variable.
super can be used to invoke parent
class method.
super is used to invoke parent class
constructor.
Super reference in a constructor
must be used in the first statement
of the constructor of subclass.
SUPER AND SUB CLASS EXAMPLE
STATIC VALUES DYNAMIC VALUES

Object is the name of the class that


is the ancestor to every other class
in java
emp1 and emp2 are reference to the
object of the class and you can
create as many objects as the
program needs.
POLYMORPHISM Polymorphic Array (Accessing overridden method)

Polymorphism is when a single variable is used with


several different types of related objects at different
places in a program.
INTERFACE
An Interface is a collection of constants and method
declarations.
An Interface same as the abstract doesn’t have method
bodies, the implementing sub class should implement
the method body having the same method signature.
An interface can contain any number of methods.
You cannot instantiate an interface.
All of the methods in an interface are abstract.
An interface cannot contain instance fields. The only
fields that can appear in an interface must be declared
both static and final.
An interface can extend multiple interfaces.
An interface is implicitly abstract and methods in an
interface are implicitly public as well.
When a class implements an interface all methods
should be overridden.
A class can implement more than one interface at a
time.
A class can extend only one class, but implement many
interfaces.
An interface can extend another interface, in a similar
way as a class can extend another class.
ABSTRACT CLASS

An abstract is a class that cannot be instantiated and it


needs to be extended and its method implemented.
An abstract class is denote by the keyword abstract it
defines both an abstract method, and an abstract class.
If there is an abstract method in a class, that class must
be abstract
A parent abstract class can have both abstract child
class and non-abstract child class.
EXERCISES ON CLASSES
EXERCISES ON CLASSES
EXERCISES ON CLASSES
EXERCISES ON CLASSES
EXERCISES ON CLASSES
EXERCISES ON CLASSES
EXERCISES ON CLASSES
EXERCISES ON CLASSES
EXERCISES ON CLASSES
EXERCISES ON COMPOSITION
EXERCISES ON COMPOSITION
EXERCISES ON COMPOSITION
EXERCISES ON COMPOSITION
EXERCISES ON COMPOSITION
EXERCISES ON COMPOSITION
EXERCISES ON COMPOSITION
EXERCISES ON COMPOSITION
EXERCISES ON COMPOSITION
EXERCISES ON INHERITANCE
EXERCISES ON INHERITANCE
EXERCISES ON INHERITANCE
EXERCISES ON
INHERITANCE
EXERCISES ON
POLYMORPHISM,
ABSTRACT CL ASSES
AND INTERFACES

You might also like