Online & Offline Java Programming Training-PSK Technologies Pvt. Ltd. Nagpur

Page 1

CORE JAVA PSK TECHNOLOGIES PVT. LTD. Plot No-780, Near Durga Temple, Katol Road Chaoni, Nagpur-13 Phone: 9975288300 / 9970141466 Email: info@psktechnologies.co.in website: www.pskitservices.com


CONTENT  Method Overriding and overloading  Final and Instance of keyword  String Handling

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


ď ś Method Overriding and overloading ďƒ˜ Polymorphism in java Polymorphism in java is a concept by which we can perform a single action by different ways. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms. There are two types of polymorphism in java: compile time polymorp hism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding. If you overload static method in java, it is the example of compile time polymorphism. Here, we will focus on runtime polymorphism in java.

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


ďƒ˜ Runtime Polymorphism in Java Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time. In this process, an overridden method is called through the r eference variable of a superclass. The determination of the m ethod to be called is based on the object being referred to by the r eference variable.

ďƒ˜ Up Casting When reference variable of Parent class refers to the object of Child class, it is known as up casting. class Bike{ void run(){System.out.println("running");} } 47 class Splender extends Bike{ void run(){System.out.println("running safely with 60km");} public static void main(String args[]){ Bike b = new Splender();//upcasting b.run(); }} Website: www.pskitservices.com

Phone: 9975288300 / 9970141466


ďƒ˜ Java Runtime Polymorphism With Multilevel Inheritance class Animal { void eat(){System.out.println ("eating");} } class Dog extends Animal { void eat(){System.out.println ("eating fruits"); }} class BabyDog extends Dog{ void eat(){System.out.println ("drinking milk"); }

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


/*BabyDog is not overriding the eat() method, so eat() method of Dog class is invoked.*/ class Animal{ void eat(){System.out.println("animal is eating...");} } class Dog extends Animal{ void eat(){System.out.println("dog is eating...");} } class BabyDog1 extends Dog{ public static void main(String args[]){ Animal a=new BabyDog1(); a.eat(); } }

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


ď ś Final and Instance Of Keyword ďƒ˜ Instance of The java instance of operator is used to test whether the object is an instance of the specified type (class or subclass or interface). The instance of in java is also known as type comparison operator because it compares the instance with type. It returns either true or false. If we apply the instance of operator with any variable that has null valu e, it returns false.

Program: class Simple1 { public static void main(String args[]) { Simple1 s=new Simple1(); System.out.println(s instanceof Simple1);//true } }

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


 Use of ‘instanceof’ Keyword  Program: /*Understanding Real use of instanceof in java*/ interface Printable{ } class A implements Printable 50{ public void a(){ System.out.println("a method"); }} class B implements Printable{ public void b(){ System.out.println("b method"); }} class Call{ void invoke(Printable p) {//upcasting if(p instanceof A) {

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


ďƒ˜ Final Keyword in Java The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be: 1. variable 2. method 3. class The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. Let's first learn the basics of final keyword.

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


ďƒ˜ Java Final Variable If you make any variable as final, you cannot change the value of final variable (It will be constant). class Bike9{ final int speedlimit=90;//final variable void run(){ speedlimit=400; } public static void main(String args[]){ Bike9 obj=new Bike9(); obj.run(); } }//end of class class Bike{ final void run(){System.out.println("running");} } 53 class Honda extends Bike{ void run(){System.out.println("running safely with 100kmph");} Website: www.pskitservices.com Phone: 9975288300 / 9970141466


public static void main(String args[]){ Honda honda= new Honda(); honda.run(); }} class Honda1 extends Bike{ void run(){System.out.println("running safely with 100kmp h");} public static void main(String args[]){ Honda1 honda= new Honda(); honda.run(); }}

ďƒ˜ Is final method inherited? Yes, final method is inherited but you cannot override it. class Bike{ final void run(){System.out.println("running...");}} class Honda2 extends Bike{ public static void main(String args[]){ new Honda2().run(); }}

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


ď ś STRING HANDLING ďƒ˜ String Handling in Java The basic aim of String Handling concept is storing the string data in he main memory (RAM), manipulating the data of the String, and etrieving the part of the String etc. String Handling provides a lot of oncepts that can be performed on a string such as concatenation of tring, comparison of string, find sub string etc.

t r c s

Java String contains an immutable sequence of Unicode characters. Java String is differ from string in C or C++, where (in C or C++) string is simply an array of char. String class is encapsulated under java.lang package. Website: www.pskitservices.com Phone: 9975288300 / 9970141466


ďƒ˜ Java String Class Methods The java.lang.String class provides many useful methods to perform operations on sequence of char values. No 1 2 3 4 5 6 7 8

Method

Description Returns char value for char char At(int index) The particular index int length() returns string length staticStringformat(String format, Returns a formatted Object... args) string. staticStringformat(Localel,String Returns formatted string format, Object... args) with given locale. Returns substring for String substring(int begin Index) given begin index Returns substring for String substring(int begin Index, int given begin index and end Index) end index. Returns true or false after boolean contains(Char Sequences) matching the Ssequence of char value. staticStringjoin(CharSequence delimiter, Char Sequence... Returns a joined string. elements)


10

boolean equals(Object another)

11

Boolean is Empty()

12

String concat(String str)

13

String replace(char old, char new)

14

String replace(Char Sequence old, Char Sequence new)

15

static String equals Ignore Case(String another)

16

String[] split(String regex)

17

String[] split(String regex, int limit)

18

String intern()

19

int index Of(int ch)

Checks the equality of string with the given object. Checks if string is empty. Concatenates the specified string. Replaces all occurrences of the specified char value. Replaces all occurrences of the specified Char Sequence. Compares another string. It doesn't check case. Returns a split string matching regex. Returns a split string matching regex and limit Returns an interned string. Returns the specified Char value index.


20

int index Of(int ch, int from Index)

21

int index Of(String substring)

22

int index Of(String substring, int from Index)

23

String to Lower Case()

24

String to Lower Case(Locale l)

25

String to Upper Case()

26

String to Upper Case(Locale l)

27

String trim()

28

static String value Of(int value)

Returns the specified char value index starting with given index. Returns the specified substring index. Returns the specified substring index starting with given index. Returns a string in lowercase. Returns a string in lowercase using specified locale. Returns a string in uppercase. Returns a string in uppercase using specified locale. Removes beginning and ending spaces of this string. Converts given type into string. It is an overloaded method.


ďƒ˜ Program: // Demonstrate indexOf() and lastIndexOf(). class indexOfDemo{ public static void main(String args[]) { String s = "Now is the time for all good men " +o come to the aid of their country."; System.out.println(s); System.out.println("indexOf(t) = " +s.indexOf('t'));System.out.println("lastIndexOf( t) = " +s.lastIndexOf('t')); System.out.println("indexOf(the) = " +s.indexOf("the")); System.out.println("lastIndexOf(the) = " +s.lastIndexOf("the")); System.out.println("indexOf(t, 10) = "

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


ďƒ˜ Program // Demonstrate append(). class appendDemo { public static void main(String args[]) { String s; int a = 40; StringBuffer sb = new StringBuffer(40); // s = sb.append("a = ").append(a).append("!").toString(); s = sb.append("a = ").append(a) .toString(); System.out.println(s); }}

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


ďƒ˜ Program // StringBuffer length vs. capacity. class StringBufferDemo { public static void main(String args[]) { /*StringBuffer() The default constructor (the one with no parameters) reserves room for 16 characters without reallocation. StringBuffer is mutable*/ StringBuffer sb = new StringBuffer("Hello"); System.out.println("buffer = " + sb); System.out.println("length = " + sb.length()); System.out.println("capacity = " + sb.capacity()); }}

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


OUR SOFTWARE COURSES

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


OUR HARDWARE COURSES HARDWARE

NETWORKING

MCITP

CCNA

CCNP

LINUX

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


OUR SERVICES WEBSITE DESIGNING & DEVELOPMENT

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


IT TRAINING

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


DIGITAL MARKETING

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


LAPTOP & DESKTOP SALES AND SERVICES

Website: www.pskitservices.com Phone: 9975288300 / 9970141466


PSK TECHNOLOGIES PVT. LTD. IT COMPANY

FOLLOW US ON: Address: Plot no-780, Near Durga Temple, Katol Road Chhaoni, Nagpur-13 https:/www.pskitservices.com Contact: 9975288300


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.