Closer look at classes Overloading Methods Overloading Constructor Objects as parameter to methods Objects as parameter to constructor Returning Objects Recursion String Class String Buffer Class Command line arguments Access Controle Static Keyword Usage Final Keyword Usage
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Closer look at classes
Overloading MethodsOverloading Constructor
Objects as parameter to methodsObjects as parameter to constructor
Returning ObjectsRecursion
String ClassString Buffer Class
Command line argumentsAccess Controle
Static Keyword UsageFinal Keyword Usage
Overloading methods• Overloading is ability of one function to perform different tasks.
• it allows creating several methods with the same name which differ from each other in the type of the input and the output
class Demo_class
{ /* having same method name*/
void demo()
{ System.out.println("hello world"); }
void demo(int a)
{ System.out.println("the value of a::"+a); }
public static void main(String[] y)
{ Demo_class d=new Demo_class();
d.demo();
d.demo(10);
}
}
Overloading Constructor• In addition to overloading normal methods,you can also overload
this.width=width;}void print_values(Rectangle r){ System.out.println("the value of member length "+r.lenght+"\n the value of member variable width is::"+this.width);}
}public class Demo_object_paramter{ public static void main(String[] y)
Returning objects• A method can return any type of data,including class types that you create
class Rectangle
{ int length,width;
Rectangle demo(int length,int width)
{ this.length=length;
this.width=width;
return(this);
}
}
public class Demo_object_paramter
{ public static void main(String[] y)
{ Rectangle r=new Rectangle();
Rectangle r1=r.demo(30,40);
System.out.println("the value of length is::"+r1.length+"\n the value of width is::"+r1.width);
}
}
Recursion• Recursion is a basic programming technique you can use in Java, in which a
method calls itself to solve some problem.
• A method that uses this technique is recursive.
class Factorial
{ int fact(int n)
{ int result;
if(n==1)
return 1;
result=fact(n-1)*n;
return result;
}
}
public class Recursion_1
{ public static void main(String[] y)
{ Factorial f=new Factorial();
System.out.println("the factorial value is::"+f.fact(5));
}
}
String Class• The first thing to understand about strings is that every string you create is
actually an object of type string . Every string constant is actually a string Object.
• Eg System.out.println(“hello world”);
• The secound thing to understand about strings is that objects of type String are immutable once string objects are created ,its content can not be altered.
D:\softwares\java_programs>java Private_AccessDemo h e l l o w
the commandline arguments
the y[0]::h the y[1]::e the y[2]::l the y[3]::l the y[4]::o
the y[5]::w
Access Control• encapsulation provides another important attribute: access control.
• Java’s access specifiers are public, private, and protected
• Public: A class, method, constructor, interface etc declared public can be accessed from any other class. Therefore fields, methods, blocks declared inside a public class can be accessed from any class
• private: Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself. Private access modifier is the most restrictive access level. Class and interfaces cannot be private
• protect: Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. The protected access modifier cannot be applied to class and interfaces.
• Default: access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.A variable or method declared without any access control modifier is available to any other class in the same package.
Public access specifierpackage demo_package;import java.util.*;public class AccessDemo{ public void test()
{ System.out.println("Example of public access specifier"); } protected int x=200;
public class Public_AccessDemo{ public static void main(String[] y)
{ AccessDemo d=new AccessDemo();d.test();
} }
####output####D:\softwares\java_programs>javac Public_AccessDemo.javaD:\softwares\java_programs>java Public_AccessDemoExample of public access specifier
Private Access modifyerclass demo{ private int x=20;
private void demo(){ System.out.println("the value of x is::"+x);}void demo_1(){ System.out.println("the demo_1 value of x is::"+x);}
}public class Private_AccessDemo{ public static void main(String[] y)
{ demo d=new demo();System.out.println("the value of x is"+d.x);d.demo();d.demo_1();
} }####output####D:\softwares\java_programs>javac Private_AccessDemo.javaerror: x has private access in demo
System.out.println("the value of x is"+d.x);: error: demo() has private access in demo
d.demo()making the d.demo_1() above two lines as comment then following out put will getD:\softwares\java_programs>java Private_AccessDemothe demo_1 value of x is::20
Protected access specifyerimport java.util.*;
import demo_package.AccessDemo;
class demo
{ protected int x=20;
protected void demo()
{ System.out.println("the value of x is::"+x); }
}
public class Private_AccessDemo extends AccessDemo
{ public static void main(String[] y)
{ demo d=new demo(); d.x=30;
System.out.println("the value of x is "+d.x); d.demo();
/*try with this code to know the difference with out extends AccessDemo
StaticVariable declaration with Static• When a variable is declared with the keyword “static”, its called a “class variable”.• All instances share the same copy of the variable. • A class variable can be accessed directly with the class, without the need to create a instance.class static_{ static String y="i am static variable";
String y1="hello_world";}public class Static_Demo{ public static void main(String[] y)
{ System.out.println("accessing static variable::"+static_.y);static_ s=new static_();s.y="hello";s.y1="H";//static_.y="done";System.out.println("accessing static variable::"+static_.y);/** here y shares the same copy*/static_ s1=new static_();System.out.println("accessing static variable::"+s1.y+"\n the value of y1::"+s1.y1);
} }
Method declaration with static• It is a method which belongs to the class and not to the object(instance)
• A static method can access only static data. It can not access non-static data (instance variables)
• A static method can call only other static methods and can not call a non-static method from it.
• A static method can be accessed directly by the class name and doesn’t need any object
• Syntax : <class-name>.<method-name>
• A static method cannot refer to “this” or “super” keywords in anyway