2 Object Oriented Programming
2 Object Oriented Programming
phone 9862145256
Basic 9000
salary
educati
on
experie
nce
Access Modifier within class within package outside package outside package
by subclass only
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
//save by A.java
package pp;
public class A{
public void msg(){
System.out.println("Hello");}
}
Prof. Amruta Deshmukh
16
2//save by B.java
To Compile :D:\>javac -d . B.java
To Run : D:\>java pack.B
package pack;
import pp.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
System.out.println("Hello java");}
obj.msg();
}
Prof. Amruta Deshmukh
} 18
3)Protected
• Protected variables & methods are accessible by the
package classes and any subclasses that are in other
packages.
//save by D:\>javac -d . Ap.java
package packAp;
public class Ap{
protected void
msg(){System.out.println("Hello");}
}
• s1=null;
• s2=null;
• System.gc();
• }
• }
Prof. Amruta Deshmukh
25
• Java System gc() Method
• The gc() method of System class runs the garbage collector.
• this method, JVM makes best effort to reuse the memory of discarded
objects for quick reuse.
• Syntax
• public static void gc() ;
• Example
• package amruta;
• public class SystemGCExample1 {
• }
Prof. Amruta Deshmukh
26
Constructor
• A constructor is a special member function used to initialize the
values of the attributes of an object.
• This function is implicitly called when an object is created.
• It is not compulsory to define a constructor. In this case the
compiler provides a default constructor but the attributes are
initialized to default values. E.g. Zero for numeric type, null for
object references and false for boolean.
• A constructor in Java is a special method that is used to
initialize objects. The constructor is called when an
object of a class is created.
class ACD{
ACD()
{
System.out.println("Default constructor");
}
public static void main(String args[])
{
ACD b=new ACD();
} Prof. Amruta Deshmukh
} 30
Java Parameterized Constructor
class Student4{
int id;
String name;
Student4(int i,String n)
{
id = i;
name = n;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[]){
• }
• class A
• {
• class B
• {
• void show()
• {
• System.out.println("Class B");
• }
• }
• }
• class MainC
• {
• public static void main(String argc[])
• {
• A a1=new A();
• A.B b2=a1.new B();
• b2.show();
• }
• }
Prof. Amruta Deshmukh
36
Types of Inner Classes
1.Nested Inner Class/Member Inner Class
2.Static Inner Classes
3.Method Local Inner Classes
4. Anonymous Inner Classes
• }
Prof. Amruta Deshmukh
• } 38
• 2) Static Nested Classes:
• In Java, we can also define a Static class inside another class. Such class
is known as Static nested class.
• Example.
• class MotherBoard {
• }
• public class staticinnerclass {
• }
Prof. Amruta Deshmukh
39
1Nested Inner Class/Member Inner Class
• In Java, you can define a class within another class. Such class
is known as nested inner class.
• 2) Static Nested Classes:
• In Java, we can also define a Static class inside another class.
Such class is known as Static nested class.
•}
• class A
• {
• void show()
• {
• System.out.println("Class A");
• }
• }
• class M1
• {
• static A a1=new A()
• {
• void show()
• {
• System.out.println("Class New");
• }
• };
•
• public static void main(String argc[])
• {
• a1.show();
• }
• }
Prof. Amruta Deshmukh
47
Abstract class in Java
• A class which is declared as abstract is known as an abstract class.
• It can have abstract and non-abstract methods.
• An abstract is a java modifier applicable for classes and methods in
java but not for Variables.
• It cannot be instantiated.
• Abstract class: is a restricted class that cannot be used to create
objects (to access it, it must be inherited from another class).
• Java Abstraction
• The major use of abstract classes and methods is to achieve
abstraction in Java.
• Abstraction is an important concept of object-oriented programming
that allows us to hide unnecessary details and only show the needed
information.
•
• }
Prof. Amruta Deshmukh
51
Example Abstract Class With Non Abstract Method and
Abstract Method
• package deshmukh;
• abstract class Book {
• public abstract void show();
• // method of abstract class
• public void display() {
• System.out.println("This is Java Programming");
• }
• }
• public class abstractclass extends Book {
• public void show()
• {
• System.out.println("abstract method");
• }
• public static void main(String[] args) {
• // TODO Auto-generated method stub
• abstractclass obj=new abstractclass();
• obj.show();
• obj.display();
• }
Prof. Amruta Deshmukh
• }
52
Abstract method:
• A method declared using the abstract keyword within an abstract
class and does not have a definition (implementation) is called an
abstract method.
• can only be used in an abstract class, and it does not have a body.
The body is provided by the subclass (inherited from).
• An abstract method must be declared with an abstract keyword.
retun_type methodname1(parameterlist);
•}
Prof. Amruta Deshmukh
66
• Example 2 interface
• interface xyz
• {
• void show();
• }
• }
• interface interfaceY1 extends interfaceX1
• {
• void print1();
• }
• public class classInterface implements interfaceY1
• {
• public void show()
• {
• System.out.println("Base interface in interfaceX1");
• }
• public void print1()
• {
• System.out.println("Derived interface in interfaceY1");
• }
Prof. Amruta Deshmukh
70
• public static void main(String[] args) {
• // TODO Auto-generated method stub
• classInterface obj=new classInterface();
• obj.show();
• obj.print1();
•}
•}
• interface interface1
•{
• void show1();
•}
• interface interface2
•{
• void show2();
•}
• }
• public class MI implements interfaceAa , interfaceBb {
• public void show() {
• System.out.println("2 Base Interface method is same ");
• }
•
• public static void main(String[] args) {
• // TODO Auto-generated method stub
• MI obj=new MI();
• obj.show();
• }
• }
Prof. Amruta Deshmukh
75
Interface extends multiple interfaces
• interface interfaceA1
• {
• void show1();
• }
• interface interfaceK1
• {
• void show2();
• }
• interface interfaceC extends interfaceA1,interfaceK1
• {
• void show3();
•}
Abstract class can have abstract and Interface can have only
non-abstract methods. abstract methods.
3) Abstract class can have final, non- Interface has only static and final
final, static and non-static variables.
variables.
4) Abstract class can provide the Interface can't provide the
implementation of interface. implementation of abstract class.
• package deshmukh;
•}
Prof. Amruta Deshmukh
89
Java Recursion Example 2: Limited times
• package deshmukh;
• }
Prof. Amruta Deshmukh
90
Static
• The main purpose of using the static keyword is to manage the
memory so that we can use the memory efficiently
• The static keyword in Java is mainly used for memory management.
• We can apply static keyword with variables, methods, blocks
and nested classes.
• The static keyword in Java is used to share the same variable or
method of a given class.
• The static keyword is used for a constant variable or a method that is
the same for every object of a class.
•{
• //body of the function
•}
• Calling Static Function
• In Java, we cannot call the static function by using the object. It is
invoked by using the class name.
• [class name].[method name]
• package deshmukh;
• }
Prof. Amruta Deshmukh
96
Example Static Method Call Another Class
• package deshmukh;
• class Adder{
• static int add(int a,int b)
• {
• return a+b;
• }
• }
• }
• static
• {
• // Print statement
• System.out.println("Static Block ");
•
• }
• public static void main(String[] args) {
• // TODO Auto-generated method stub
• System.out.println("Main Method ");
• }
• }
Prof. Amruta Deshmukh
99
Execution Order:
• }
Prof. Amruta Deshmukh
101
Use of “this” reference
• ‘this’ is a keyword in java.
• ‘this’ points to the current object.
• ‘this’ always holds address of an object which invokes the member
function.
• The keyword this can be used inside any method to refer to the current
object.
• Box(double w, double h, double d) {
this.width=w; this.height=h;
this.depth=d; }
Inside Box(), ‘this’ always refers to the invoking object.
The most common use of the this keyword is to eliminate the confusion
between class attributes and parameters with the same name .
• }
Cloned object and the original Cloned object and the original
Prof. Amruta Deshmukh
object are not disjoint. object are disjoint.
116
Advantage of OOPs over Procedure-oriented
programming language
1) OOPs make development and maintenance easier where as in
Procedure-oriented programming language it is not easy to manage if
code grows as project size grows.
2) OOPs provide data hiding whereas in Procedure-oriented
programming language a global data can be accessed from anywhere.
3) OOPs provide ability to simulate real-world event much more
effectively. We can provide the solution of real word problem if we
are using the Object-Oriented Programming language.