instanceof java
Posts: 576
Nickname: instanceof
Registered: Jan, 2015
|
instanceof java is a java related one.
|
|
|
|
This and Super Keywords
|
Posted: Mar 8, 2015 6:20 AM
|
|
|
This post originated from an RSS feed registered with Java Buzz
by instanceof java.
|
Original Post: This and Super Keywords
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
|
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java
|
|
- this keyword used to store current object reference.
super keyword used to store super class reference in subclass object.
this keyword: - Predefined instance variable to hold current object reference.
It must used explicitly if non-static variable and local variable name is same.- package com.instanceofjava;
-
- public class ThisDemo {
- int a, b;
-
- ThisDemo(int a, int b){
-
- a=a;
- b=b;
- }
-
- public static void main(String[] args){
-
- ThisDemo obj= new ThisDemo(1,2);
-
- System.out.println(obj.a);
- System.out.println(obj.b);
- }
- }
Output:- Without this keyword if variable names are same we can not assign instance variables so this problem will solve by using this keyword
- package com.instanceofjava;
-
- public class ThisDemo {
- int a, b;
-
- ThisDemo(int a, int b){
-
- this.a=a;
- this.b=b;
- }
-
- public static void main(String[] args){
-
- ThisDemo obj= new ThisDemo(1,2);
-
- System.out.println(obj.a);
- System.out.println(obj.b);
- }
- }
Output:Used to invoke current class constructor:- package com.instanceofjava;
-
- public class ThisDemo {
- int a, b;
-
- ThisDemo(){
- System.out.println("Default constructor called");
- }
- ThisDemo(int a, int b){
- this();
- this.a=a;
- this.b=b;
- }
-
- public static void main(String[] args){
-
- ThisDemo obj= new ThisDemo(1,2);
-
- System.out.println(obj.a);
- System.out.println(obj.b);
- }
- }
Output:- Default constructor called
- 1
- 2
Used to call current class method: - package com.instanceofjava;
-
- public class Sample{
- int a, b;
-
- Sample(int a, int b){
-
- this.a=a;
- this.b=b;
- }
-
- void show(){
-
- System.out.println("Show() method called");
-
- }
-
- void print(){
-
- this.show();
- System.out.println(obj.a);
- System.out.println(obj.b);
-
- }
- public static void main(String[] args){
-
- Sample obj= new Sample(1,2);
-
-
- obj.print()
- }
- }
Output:super keyword:- Predefined instance variable used to hold super class object reference through sub class object.
Used to call super class constructor:- package com.instanceofjava;
-
- public class A{
- int a, b;
-
- A(int a, int b){
-
- a=a;
- b=b;
- System.out.println("super class constructor called");
-
- }
-
- }
- package com.instanceofjava;
-
- public class B extends A{
- int a, b;
-
- A(int a, int b){
- super(a,b);
- a=a;
- b=b;
- }
- public static void main(String[] args){
-
- B obj= new B(1,2);
-
-
- }
-
- }
-
- }
Output:- super class constructor called
- 1
- 2
Used to call super class methods from subclass:- package com.instanceofjava;
-
- public class A{
- int a, b;
-
- A(int a, int b){
-
- a=a;
- b=b;
- System.out.println("super class constructor called");
-
- }
-
- void add(){
- System.out.println("super class method called");
- }
-
- }
- package com.instanceofjava;
-
- public class B extends A{
- int a, b;
-
- A(int a, int b){
- super(a,b);
- a=a;
-
- }
- void print(){
- super.add();
- System.out.println(this.a);
- System.out.println(this.b);
-
- }
- public static void main(String[] args){
-
- B obj= new B(1,2);
-
- obj.print();
- }
-
- }
-
- }
Output:- super class method called
- 1
- 2
Read: This and Super Keywords
|
|