4. how arguments are passed to a method
• ArraysToMethod(File:ArraysToMethod)
5. encapsulation
• Wrapping up of data under a single unit.
• Encapsulation can be achieved by declaring all
the variables in the class as private and writing
public methods in the class to set and get the
values of variables.
• Example(File: encapsulationEx)
6. access control in Java
Access
Modifier
within class within
package
outside
package by
subclass only
outside
package
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
7. overloading constructor
• Similar like method overloading, you can also
overload constructors if two or more
constructors are different in parameters.
Example(File:OverloadingConstructor)
8. • A Java constructor name must exactly match with the
class name (including case).
• A Java constructor must not have a return type.
• If a class doesn't have a constructor, Java compiler
automatically creates a default constructor during
run-time. The default constructor initialize instance
variables with default values. For
example: int variable will be initialized to 0
• Constructors cannot be abstract or static or final.
• Constructor but can not be overridden. can be
overloaded
9. Constructor
Types:
– No-Arg Constructor - a constructor that does not
accept any arguments
– Default Constructor - a constructor that is
automatically created by the Java compiler if it is
not explicitly defined.
– Parameterized constructor - used to specify
specific values of variables in object
10. Can a Constructor return any value ?
• The answer is the Constructor cannot
return any explicit value but implicitly it will
be returning the instance of the class.
11. • Access Modifier:- Java provides both Access Specifier and Access
Modifiers for creating access to your Java code for other classes. Here
modifier is also used to do the same task but there are limitations.
• Class Modifier:
– abstract :- This defines the restriction such that objects cannot be created.
– final:- This restricts a class from being inherited.
– strictfp:- it is related to the checking of floating point values irrespective of OS.
• Variable Modifier:
– static:no object creation required
– final: cannot be reassigned
– transient: it is not serialized
– volatile: the values are liable for change
12. Nested classes, inner classes
Define a class within another class. Such a class
is called a nested class or inner class.
class OuterClass
{
class NestedClass {
}
}
13. Nested classes are divided into two categories:
• static nested class : Nested classes that are
declared static are called static nested classes.
• non-static nested class : An inner class is a
non-static nested class.
16. instantiate an inner class
• To instantiate an inner class, you must first
instantiate the outer class. Then, create the
inner object within the outer object with this
syntax:
OuterClass outerObject=new OuterClass();
OuterClass.InnerClass innerObject =
outerObject.new InnerClass();
17. static methods
• Static methods are the methods in Java that
can be called without creating an object of
class. They are referenced by the class name
itself .
ClassName.methodName(args)
Example(File: staticMethodEx)
18. Instance method vs Static method
• Instance method can access the instance
methods and instance variables directly.
• Instance method can access static variables
and static methods directly.
• Static methods can access the static variables
and static methods directly.
• Static methods can’t access instance methods
and instance variables directly. They must use
reference to object.
• static method can’t use this keyword