Best Java Programming training in Nagpur

Page 1

CORE JAVA PSK TECHNOLOGIES An ISO 9001:2015 (QMS) Certified IT Company Computer Education | Software Development | Computer Sales & Services Plot No-780, Near Durga Temple, Katol Road Chaoni, Nagpur-13 Phone: 9975288300 / 9970141466 Email: info@psktechnologies.co.in website: www.pskitservices.com


CONTENT  Constructor 

Static variable

This Keywords

Inheritance

Abstraction & Interface

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


 Constructor A constructor is a ‘special’ member function whose task is to initialize the objects of its class. It is special because its name is the same as the class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it construct the values of data member of the class. A constructor is declared and defined as follows: //class with a constructor Class integer { int m,n; Public: integer (void); //constructor declared ……. ……. }; Integer:: integer (void) //constructor defined { m= 0; n= 0; }

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


When a class contains a constructor like the one defined above, it is guaranteed that an object created by the class will be initialized automatically. For example, the declaration Integer int1; // object int1 created. Not only created the object int1 of the integer but also initializes its data member’s m and n to zero. There is no need to write any statement to invoke. the constructor function (as we do with the normal member function). If a ‘normal’ member function is defined for zero initialization, we would need to invoke this function for each of the objects separately. This would be very inconvenient, if there are a larger number of objects. A constructor that accepts no parameters is called the default constructor. The default constructor for class A is A::A (). If no such constructor is defined, then the compiler supplies a default constructor.

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


ďƒ˜ Program: class Student4 { int id; String name; Student4(int i,String n) { id = i; name = n; } void display() { System.out.println("Id="+id+"\t"+"Name="+name); } public static void main(String args[]) { Student4 s1 = new Student4(111,"Karan");

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


ďƒ˜ Program: Constructor overloading class Student5 { int id; String name; int age; Student5(int i,String n) { id = i; name = n; } Student5(int i,String n,int a) { id = i; name = n; age=a; }

void display() { System.out.println(id+" "+name+" "+age); } public static void main(String args[]) { Student5 s1 = new Student5(111,"Karan"); Student5 s2 = new Student5(222,"Aryan",25); s1.display(); s2.display(); } }

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


 Copy Constructor Java Copy Constructor There is no copy constructor in java. But, we can copy the values of one object to another like copy constructor in C++. There are many ways to copy the values of one object into another in java. They are: 

By constructor

By assigning the values of one object into another

By clone() method of Object class*/

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


ďƒ˜ Program: class Student6 { int id; String name; Student6(int i,String n) { id = i; name = n; } Student6(Student6 s) { id = s.id; name =s.name; }

void display() { System.out.println(id+" "+name); } public static void main(String args[]) { Student6 s1 = new Student6(111,"Karan"); Student6 s2 = new Student6(s1); s1.display(); s2.display(); } }

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


 STATIC VARIABLE  Static Member Function 1) Like static member variable we can also have static member function. 2) A static function can have access to only other static member (function or variable) declared in same class. 3) A static member function can be called using the class name. class_name :: function_name;

()

When counter () function is called and gets destroyed each time when counter () function ends.But, if we makes it static, once initialized count will have a scope till the end of main function and it will carry, its value through function calls too. If you don’t initialized a static variable, they are by default initialized to zero.

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


ďƒ˜ Program: class Counter2 { static int count; //will get memory only once and retain its value Counter2() { count++; System.out.println(count); } /*void display() { System.out.println("\n\n\n"+count); }*/

public static void main(String args[]) { Counter2 c1=new Counter2(); Counter2 c2=new Counter2(); Counter2 c3=new Counter2(); /*c1.display(); c2.display(); c3.display();*/ }

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


ďƒ˜ Why Java Main Method Is Static? Because object is not required to call static method if it were non-static method, jvm create object first then call main() method that will lead the problem of extra memory allocation. Java static block: 1) Is used to initialize the static data member. 2) It is executed before main method at the time of class loading class A2 { static { System.out.println("static block is invoked"); } public static void main(String args[]) { System.out.println("Hello main"); }}

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


 THIS KEYWORD 

Definition of This

To refer the current class instance variable to invoke the current class constructor to invoke the current class method to pass as an argument in the method call to pass as an argument in the constructor call to return the current class instance proving this keyword there can be a lot of usage of java this keyword. In java, this is a reference variable that refers to the current object.

Using ‘this ‘keyword

Here is given the 6 usage of java this keyword. 1.this keyword can be used to refer current class instance variable. 2.this() can be used to invoke current class constructor. 3.this keyword can be used to invoke current class method (implicitly) 4.this can be passed as an argument in the method call. 5.this can be passed as argument in the constructor call. 6.this keyword can also be used to return the current class instance

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


ďƒ˜

Program:

class This1 { int id; String name; This1(int id,String name) { this.id = id; this.name = name; } void display() { System.out.println(id+" "+name); }

public static void main(String args[]) { This1 s1 = new This1(11,"Karan"); This1 s2 = new This1(22,"Aryan"); s1.display(); s2.display(); } }

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


 Constructor Call Using ‘This’ This() can be used to invoked current class constructor. The this() constructor call can be used to invoke the current class constructor (constructor chaining). This approach is better if you have many constructors in the class and want to reuse that constructor.

 Program: class ThisCon {int id; String name; ThisCon() {System.out.println("default constructor is invoked"); ThisCon(int id,String name) { this (); //it is used to invoked current class constructor. this.id = id; this.name = name; }

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


ďƒ˜ Program: This keyword refers to the current class instance variable. In this program, we are printing the reference variable and this, output of both variables are same class A5 { void m() { System.out.println(this);//prints same reference ID } public static void main(String args[]) { A5 obj=new A5(); //System.out.println(obj); //prints the reference ID obj. m(); } }

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


ďƒ˜ Inheritance Why multiple inheritance is not in java.

ďƒ˜ Program: class A { A() { System.out.println("A constructor called"); System.out.println("asd"); } } class B extends A { B()

{ System.out.println("B constructor called"); } } class Inh1 { public static void main(String[] args) { B b=new B(); } }

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


ďƒ˜ Program: class A { int a,b; void getA(int x,int y) { a=x; b=y; System.out.println("Super Add="+(a+b)); } } class B extends A { int m,n; void getB(int t,int s,int p,int q)

{ super.getA(t,s); m=p; n=q; System.out.println("Sub Add="+(m+n)); } } class Inh3 { public static void main(String[] args) { B b=new B(); b.getB(1,2,3,4); } }

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


 Abstraction & Interface  Abstract class in Java A class that is declared with abstract keyword, is known as abstract class in java. It can have abstract and non-abstract methods (method with body).Before learning java abstract class, let's understand the abstraction in java first.

 Abstraction in Java Abstraction is a process of hiding the implementation details and showing only functionality to the user. Another way, it shows only important things to the user and hides the internal details for example sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery. Abstraction lets you focus on what the object does instead of how it does it.

 Ways to achieve Abstraction There are two ways to achieve abstraction in java Abstract class (0 to 100%) Interface (100%)

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


ďƒ˜ Program: abstract class Bank { abstract int getRateOfInterest(); } class SBI extends Bank { int getRateOfInterest() { return 7; } } class PNB extends Bank { int getRateOfInterest() {

return 9; } } class TestBank { public static void main(String args[]) { Bank b=new SBI();//if object is PNB, method of PNB will be invoked int interest=b.getRateOfInterest(); System.out.println("Rate of Interest is: "+interest+" %"); } }

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


ďƒ˜ Interface With Class An interface in java is a blueprint of a class.It has static constants and abstract methods only. The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve fully abstraction and multiple inheritance in Java.

ďƒ˜ Program: interface f1 { public void get(int x,int y); } interface f2 { public void show(); } class Myclass implements f1,f2 { int a,b; public void get(int x,int y) { a=x; b=y; }

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


ďƒ˜ Program: import java.util.*; interface f1 { public void get(); final int c=5; } interface f2 { public void show(); } class Myclass implements f1,f2 { int a,b; Scanner sc=new Scanner(System.in); public void get() { System.out.println("Enter a and b="); a=sc.nextInt(); b=sc.nextInt(); } public void show() { System.out.println("Add="+(a+b+c)); }} class Intf2 { public static void main(String args[]) { Myclass ob=new Myclass(); ob.get(); ob.show();

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


OUR SOFTWARE COURSES

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


OUR HARDWARE COURSES MCITP

CCNA

NETWORKING

HARDWARE

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 SALES AND SERVICES

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


PSK TECHNOLOGIES PVT. LTD. IT COMPANY

Thank You! 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.