SlideShare a Scribd company logo
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
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
constructor methods
class Demo_Constructor_overload
{ Demo_Constructor_overload()
{ System.out.println("hello default constructor");
}
Demo_Constructor_overload(String s)
{ System.out.println("hello default constructor 1::"+s);
}
public static void main(String[] a1)
{ Demo_Constructor_overload d=new Demo_Constructor_overload();
Demo_Constructor_overload d1=new Demo_Constructor_overload
("yugandhar");
}
}
Uses of method Overloading
• The main advantage of this is cleanliness of
code.
• the use of function overloading is to save the
memory space, consistency and readabiliy.
Objects as parameters to methods
class Rectangle
{ int lenght,width;
Rectangle(int lenght,int width)
{ this.lenght=lenght;
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)
{ Rectangle r=new Rectangle(10,20);
r.print_values(r);
}
}
Objects as parameters to Constructor
class Rectangle
{ int lenght,width;
Rectangle(int lenght,int width)
{ this.lenght=lenght;
this.width=width;
}
Rectangle(Rectangle r)
{ System.out.println("the member varialbe lenght::"+r.lenght+"n the
member varialbe height::"+r.width);
}
}
public class Demo_object_paramter
{ public static void main(String[] y)
{ Rectangle r=new Rectangle(30,20);
Rectangle r1=new Rectangle(r);
}
}
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.
public class Private_AccessDemo
{ public static void main(String[] y)
{ String s=new String("yugandhar");
System.out.println("the s String is::"+s);
System.out.println("the String length::"+s.length());
String s1=new String("programmer");
System.out.println("the String concatination "+s+s1);
}
}
Declaring String Array
public static void main(String[] y)
{ String[] s=new String[3];//string array with 3
Scanner sc=new Scanner(System.in);//taking input at run time from user
for(int i=0;i<s.length;i++)
s[i]=sc.nextLine();
for(int i=0;i<s.length;i++)
System.out.println(" "+s[i]);
}
Some String Methods
public class Private_AccessDemo
{ public static void main(String[] y)
{ String s="yugandhar";
System.out.println("the upper case::"+s.toUpperCase());
System.out.println("the upper case::"+s.toLowerCase());
System.out.println("the replace ::"+s.replace('a','r'));
System.out.println("the charAt ::"+s.charAt(8));
}
}
String Buffer Class
• String Buffer creates the string of flexible length that can be modified in
terms content and length. We can insert characters and substrings in the
middle of the string are append another string at the end.
public class Private_AccessDemo
{ public static void main(String[] y)
{ StringBuffer s=new StringBuffer("yugandhar");//StringBuffer constructor
System.out.println("the String Buffer class");
s.setCharAt(6,'x');//methods of String Buffer class
System.out.println(s);
System.out.println("the append string::"+s.append(" programmer"));
s.setLength(40);// methods of String Buffer class
System.out.println("the set length ::"+s.length());
}
}
Command line arguments
• A command-line argument is the information that directly follows the
program’s name on the command line when it is excuted .
• All command line arguments are passed as strings
public static void main(String[] y)
{ System.out.println("the commandline arguments");
for(int i=0;i<y.length;i++)
System.out.println("the y["+i+"]::"+y[i]);
}
####output####
D:softwaresjava_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 specifier
package demo_package;
import java.util.*;
public class AccessDemo
{ public void test()
{ System.out.println("Example of public access specifier"); }
protected int x=200;
}
D:softwaresjava_programs>javac -d . AccessDemo.java
import java.util.*;
import demo_package.AccessDemo;
public class Public_AccessDemo
{ public static void main(String[] y)
{ AccessDemo d=new AccessDemo();
d.test();
}
}
####output####
D:softwaresjava_programs>javac Public_AccessDemo.java
D:softwaresjava_programs>java Public_AccessDemo
Example of public access specifier
Private Access modifyer
class 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:softwaresjava_programs>javac Private_AccessDemo.java
error: 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 get
D:softwaresjava_programs>java Private_AccessDemo
the demo_1 value of x is::20
Protected access specifyer
import 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
AccessDemo a=new AccessDemo();
System.out.println(a.x);
-*/
Private_AccessDemo a=new Private_AccessDemo();
System.out.println(a.x);
}
}
D:softwaresjava_programs>java Private_AccessDemo
the value of x is 30 the value of x is::30 200
Static
Variable 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
class static_
{ int x=10;
static void demo()
{ System.out.println("hellow static method");
// System.out.println("the value of x is"+x);
/*
* static methods can call only other static methods
* and other static variables only
*/
//demo_1();
demo_2();
}
void demo_1()
{ System.out.println("not static method demo_1()"); }
static void demo_2()
{ System.out.println(" static method demo_2()"); }
}//class
public class Static_Demo
{ static void demo_3()
{ System.out.println("the main methods static"); }
public static void main(String[] y)
{ System.out.println("static method");
static_.demo();
demo_3();
}
}
####output####
static method
hellow static method
static method demo_2()
the main methods static
Static block
• The static block, is a block of statement inside a Java class that will be executed when
a class is first loaded in to the JVM.
class static_
{ int x=10;
void demo()
{ System.out.println("hellow"); }
static{ System.out.println("secound static block");
}
static{ System.out.println("first static block");
}
}
public class Static_Demo
{ public static void main(String[] y)
{ static_ s=new static_();
s.demo();
}
}
####output####
secound static block
first static block
hellow
final keyword
• final is a reserved keyword in Java to restrict the user and it can be applied
to member variables, methods, class and local variables.
class Demo
{ final int INT_VARIABLE=120;
/**final variable value can not be chage */
public static void main(String[] y)
{ Demo d=new Demo();
d.INT_VARIABLE=45;
}
}
####output####
C:UsersYugandharDesktopjsp>javac Demo.java
Demo.java:8: error: cannot assign a value to final variable INT_VARIABLE
d.INT_VARIABLE=45;
Final method
• A final method cannot be overridden. Which means even though a sub
class can call the final method of parent class without any issues but it
cannot override it.
class Super
{ final void demo()
{ System.out.println("hello"); }
}
class Sub_ extends Super
{ /*void demo()
{
System.out.println("hello world");
}*/
void demo_1()
{ System.out.println("hello world"); }
}
class Demo
{ public static void main(String[] y)
{ Sub_ s=new Sub_();
s.demo();
}
}
try to remove those comments to understand
####output####
C:UsersYugandharDesktopjsp>javac Demo.java
Demo.java:11: error: demo() in Sub_ cannot override demo() in Super
C:UsersYugandharDesktopjsp>java Demo
hello
Final class
• We cannot extend a final class prevent inheritance
final class Super
{ void demo()
{ System.out.println("hello"); }
}
class Sub_ extends s
{ void demo_1()
{ System.out.println("hello world"); }
}
class Demo
{ public static void main(String[] y)
{ Sub_ s=new Sub_(); }
}
####output####
C:UsersYugandharDesktopjsp>javac Demo.java
Demo.java:9: error: cannot inherit from final Super
class Sub_ extends Super
Uses of final keyword
• Using final to define constants
• Using final to prevent inheritance
• Using final to prevent overriding
• Using final for method arguments

More Related Content

What's hot

OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
Shalabh Chaudhary
 
Interface
InterfaceInterface
Interface
vvpadhu
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
mohamedsamyali
 
Java Programs
Java ProgramsJava Programs
Java Programs
vvpadhu
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
Kalai Selvi
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and Object
OUM SAOKOSAL
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
Raja Sekhar
 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4
mohamedsamyali
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
Intro C# Book
 
Java Programming - 05 access control in java
Java Programming - 05 access control in javaJava Programming - 05 access control in java
Java Programming - 05 access control in java
Danairat Thanabodithammachari
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
Intro C# Book
 
Core java
Core javaCore java
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
Raga Vahini
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
hmanjarawala
 
14. Java defining classes
14. Java defining classes14. Java defining classes
14. Java defining classes
Intro C# Book
 
Core java oop
Core java oopCore java oop
Core java oop
Parth Shah
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
sarfarazali
 
11. java methods
11. java methods11. java methods
11. java methods
M H Buddhika Ariyaratne
 

What's hot (18)

OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
Interface
InterfaceInterface
Interface
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and Object
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
 
Java Programming - 05 access control in java
Java Programming - 05 access control in javaJava Programming - 05 access control in java
Java Programming - 05 access control in java
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 
Core java
Core javaCore java
Core java
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
14. Java defining classes
14. Java defining classes14. Java defining classes
14. Java defining classes
 
Core java oop
Core java oopCore java oop
Core java oop
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
11. java methods
11. java methods11. java methods
11. java methods
 

Viewers also liked

Constructor overloading in C++
Constructor overloading in C++Constructor overloading in C++
Constructor overloading in C++
Learn By Watch
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2
Abbas Ajmal
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Dr Sukhpal Singh Gill
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
ForwardBlog Enewzletter
 
Overloading in java
Overloading in javaOverloading in java
Overloading in java
774474
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
Ashita Agrawal
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
Learn By Watch
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
 

Viewers also liked (10)

Constructor overloading in C++
Constructor overloading in C++Constructor overloading in C++
Constructor overloading in C++
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Overloading in java
Overloading in javaOverloading in java
Overloading in java
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 

Similar to Closer look at classes

Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
yugandhar vadlamudi
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
kamal kotecha
 
C#2
C#2C#2
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoM
ambikavenkatesh2
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
ROHIT JAISWAR
 
Java class
Java classJava class
Java class
Arati Gadgil
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
sotlsoc
 
L04 Software Design 2
L04 Software Design 2L04 Software Design 2
L04 Software Design 2
Ólafur Andri Ragnarsson
 
Java practical
Java practicalJava practical
Java practical
william otto
 
Lecture4
Lecture4Lecture4
Lecture4
Ravi Kant Kumar
 
Oop lecture5
Oop lecture5Oop lecture5
Oop lecture5
Shahriar Robbani
 
nested_Object as Parameter & Recursion_Later_commamd.pptx
nested_Object as Parameter  & Recursion_Later_commamd.pptxnested_Object as Parameter  & Recursion_Later_commamd.pptx
nested_Object as Parameter & Recursion_Later_commamd.pptx
Kongu Engineering College, Perundurai, Erode
 
Interface
InterfaceInterface
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
Jamie (Taka) Wang
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
BG Java EE Course
 
Lecture4
Lecture4Lecture4
Lecture4
rajesh0ks
 
Lecture4
Lecture4Lecture4
Lecture4
FALLEE31188
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
Terry Yoast
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
manish kumar
 

Similar to Closer look at classes (20)

Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
C#2
C#2C#2
C#2
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoM
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
Java class
Java classJava class
Java class
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
L04 Software Design 2
L04 Software Design 2L04 Software Design 2
L04 Software Design 2
 
Java practical
Java practicalJava practical
Java practical
 
Lecture4
Lecture4Lecture4
Lecture4
 
Oop lecture5
Oop lecture5Oop lecture5
Oop lecture5
 
nested_Object as Parameter & Recursion_Later_commamd.pptx
nested_Object as Parameter  & Recursion_Later_commamd.pptxnested_Object as Parameter  & Recursion_Later_commamd.pptx
nested_Object as Parameter & Recursion_Later_commamd.pptx
 
Interface
InterfaceInterface
Interface
 
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture4
Lecture4Lecture4
Lecture4
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 

More from yugandhar vadlamudi

Toolbarexample
ToolbarexampleToolbarexample
Toolbarexample
yugandhar vadlamudi
 
Singleton pattern
Singleton patternSingleton pattern
Singleton pattern
yugandhar vadlamudi
 
Object Relational model for SQLIite in android
Object Relational model for SQLIite  in android Object Relational model for SQLIite  in android
Object Relational model for SQLIite in android
yugandhar vadlamudi
 
JButton in Java Swing example
JButton in Java Swing example JButton in Java Swing example
JButton in Java Swing example
yugandhar vadlamudi
 
Collections framework in java
Collections framework in javaCollections framework in java
Collections framework in java
yugandhar vadlamudi
 
Packaes & interfaces
Packaes & interfacesPackaes & interfaces
Packaes & interfaces
yugandhar vadlamudi
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
yugandhar vadlamudi
 
JMenu Creation in Java Swing
JMenu Creation in Java SwingJMenu Creation in Java Swing
JMenu Creation in Java Swing
yugandhar vadlamudi
 
Adding a action listener to button
Adding a action listener to buttonAdding a action listener to button
Adding a action listener to button
yugandhar vadlamudi
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
yugandhar vadlamudi
 
Operators in java
Operators in javaOperators in java
Operators in java
yugandhar vadlamudi
 
Inheritance
InheritanceInheritance
Inheritance
yugandhar vadlamudi
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi
 
java Applet Introduction
java Applet Introductionjava Applet Introduction
java Applet Introduction
yugandhar vadlamudi
 

More from yugandhar vadlamudi (14)

Toolbarexample
ToolbarexampleToolbarexample
Toolbarexample
 
Singleton pattern
Singleton patternSingleton pattern
Singleton pattern
 
Object Relational model for SQLIite in android
Object Relational model for SQLIite  in android Object Relational model for SQLIite  in android
Object Relational model for SQLIite in android
 
JButton in Java Swing example
JButton in Java Swing example JButton in Java Swing example
JButton in Java Swing example
 
Collections framework in java
Collections framework in javaCollections framework in java
Collections framework in java
 
Packaes & interfaces
Packaes & interfacesPackaes & interfaces
Packaes & interfaces
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
 
JMenu Creation in Java Swing
JMenu Creation in Java SwingJMenu Creation in Java Swing
JMenu Creation in Java Swing
 
Adding a action listener to button
Adding a action listener to buttonAdding a action listener to button
Adding a action listener to button
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Inheritance
InheritanceInheritance
Inheritance
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
java Applet Introduction
java Applet Introductionjava Applet Introduction
java Applet Introduction
 

Recently uploaded

RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
NelTorrente
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
ArianaBusciglio
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 

Recently uploaded (20)

RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 

Closer look at classes

  • 1. 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
  • 2. 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); } }
  • 3. Overloading Constructor • In addition to overloading normal methods,you can also overload constructor methods class Demo_Constructor_overload { Demo_Constructor_overload() { System.out.println("hello default constructor"); } Demo_Constructor_overload(String s) { System.out.println("hello default constructor 1::"+s); } public static void main(String[] a1) { Demo_Constructor_overload d=new Demo_Constructor_overload(); Demo_Constructor_overload d1=new Demo_Constructor_overload ("yugandhar"); } }
  • 4. Uses of method Overloading • The main advantage of this is cleanliness of code. • the use of function overloading is to save the memory space, consistency and readabiliy.
  • 5. Objects as parameters to methods class Rectangle { int lenght,width; Rectangle(int lenght,int width) { this.lenght=lenght; 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) { Rectangle r=new Rectangle(10,20); r.print_values(r); } }
  • 6. Objects as parameters to Constructor class Rectangle { int lenght,width; Rectangle(int lenght,int width) { this.lenght=lenght; this.width=width; } Rectangle(Rectangle r) { System.out.println("the member varialbe lenght::"+r.lenght+"n the member varialbe height::"+r.width); } } public class Demo_object_paramter { public static void main(String[] y) { Rectangle r=new Rectangle(30,20); Rectangle r1=new Rectangle(r); } }
  • 7. 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); } }
  • 8. 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)); } }
  • 9. 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. public class Private_AccessDemo { public static void main(String[] y) { String s=new String("yugandhar"); System.out.println("the s String is::"+s); System.out.println("the String length::"+s.length()); String s1=new String("programmer"); System.out.println("the String concatination "+s+s1); } }
  • 10. Declaring String Array public static void main(String[] y) { String[] s=new String[3];//string array with 3 Scanner sc=new Scanner(System.in);//taking input at run time from user for(int i=0;i<s.length;i++) s[i]=sc.nextLine(); for(int i=0;i<s.length;i++) System.out.println(" "+s[i]); }
  • 11. Some String Methods public class Private_AccessDemo { public static void main(String[] y) { String s="yugandhar"; System.out.println("the upper case::"+s.toUpperCase()); System.out.println("the upper case::"+s.toLowerCase()); System.out.println("the replace ::"+s.replace('a','r')); System.out.println("the charAt ::"+s.charAt(8)); } }
  • 12. String Buffer Class • String Buffer creates the string of flexible length that can be modified in terms content and length. We can insert characters and substrings in the middle of the string are append another string at the end. public class Private_AccessDemo { public static void main(String[] y) { StringBuffer s=new StringBuffer("yugandhar");//StringBuffer constructor System.out.println("the String Buffer class"); s.setCharAt(6,'x');//methods of String Buffer class System.out.println(s); System.out.println("the append string::"+s.append(" programmer")); s.setLength(40);// methods of String Buffer class System.out.println("the set length ::"+s.length()); } }
  • 13. Command line arguments • A command-line argument is the information that directly follows the program’s name on the command line when it is excuted . • All command line arguments are passed as strings public static void main(String[] y) { System.out.println("the commandline arguments"); for(int i=0;i<y.length;i++) System.out.println("the y["+i+"]::"+y[i]); } ####output#### D:softwaresjava_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
  • 14. 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.
  • 15. Public access specifier package demo_package; import java.util.*; public class AccessDemo { public void test() { System.out.println("Example of public access specifier"); } protected int x=200; } D:softwaresjava_programs>javac -d . AccessDemo.java import java.util.*; import demo_package.AccessDemo; public class Public_AccessDemo { public static void main(String[] y) { AccessDemo d=new AccessDemo(); d.test(); } } ####output#### D:softwaresjava_programs>javac Public_AccessDemo.java D:softwaresjava_programs>java Public_AccessDemo Example of public access specifier
  • 16. Private Access modifyer class 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:softwaresjava_programs>javac Private_AccessDemo.java error: 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 get D:softwaresjava_programs>java Private_AccessDemo the demo_1 value of x is::20
  • 17. Protected access specifyer import 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 AccessDemo a=new AccessDemo(); System.out.println(a.x); -*/ Private_AccessDemo a=new Private_AccessDemo(); System.out.println(a.x); } } D:softwaresjava_programs>java Private_AccessDemo the value of x is 30 the value of x is::30 200
  • 18. Static Variable 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); } }
  • 19. 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
  • 20. class static_ { int x=10; static void demo() { System.out.println("hellow static method"); // System.out.println("the value of x is"+x); /* * static methods can call only other static methods * and other static variables only */ //demo_1(); demo_2(); } void demo_1() { System.out.println("not static method demo_1()"); } static void demo_2() { System.out.println(" static method demo_2()"); } }//class public class Static_Demo { static void demo_3() { System.out.println("the main methods static"); } public static void main(String[] y) { System.out.println("static method"); static_.demo(); demo_3(); } } ####output#### static method hellow static method static method demo_2() the main methods static
  • 21. Static block • The static block, is a block of statement inside a Java class that will be executed when a class is first loaded in to the JVM. class static_ { int x=10; void demo() { System.out.println("hellow"); } static{ System.out.println("secound static block"); } static{ System.out.println("first static block"); } } public class Static_Demo { public static void main(String[] y) { static_ s=new static_(); s.demo(); } } ####output#### secound static block first static block hellow
  • 22. final keyword • final is a reserved keyword in Java to restrict the user and it can be applied to member variables, methods, class and local variables. class Demo { final int INT_VARIABLE=120; /**final variable value can not be chage */ public static void main(String[] y) { Demo d=new Demo(); d.INT_VARIABLE=45; } } ####output#### C:UsersYugandharDesktopjsp>javac Demo.java Demo.java:8: error: cannot assign a value to final variable INT_VARIABLE d.INT_VARIABLE=45;
  • 23. Final method • A final method cannot be overridden. Which means even though a sub class can call the final method of parent class without any issues but it cannot override it. class Super { final void demo() { System.out.println("hello"); } } class Sub_ extends Super { /*void demo() { System.out.println("hello world"); }*/ void demo_1() { System.out.println("hello world"); } } class Demo { public static void main(String[] y) { Sub_ s=new Sub_(); s.demo(); } } try to remove those comments to understand ####output#### C:UsersYugandharDesktopjsp>javac Demo.java Demo.java:11: error: demo() in Sub_ cannot override demo() in Super C:UsersYugandharDesktopjsp>java Demo hello
  • 24. Final class • We cannot extend a final class prevent inheritance final class Super { void demo() { System.out.println("hello"); } } class Sub_ extends s { void demo_1() { System.out.println("hello world"); } } class Demo { public static void main(String[] y) { Sub_ s=new Sub_(); } } ####output#### C:UsersYugandharDesktopjsp>javac Demo.java Demo.java:9: error: cannot inherit from final Super class Sub_ extends Super
  • 25. Uses of final keyword • Using final to define constants • Using final to prevent inheritance • Using final to prevent overriding • Using final for method arguments