Skip to main content

Super and Final keyword

 


Super Keyword

    The super keyword in java is a reference varible which is use to refer immediate parent class instance variable. Super can use to invoke immediate parent class method and constructors also.


1. To Refer Instance variable of an immediate parent class:
    We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields. For example,
class Animal{  
String color="white";  
}  
class Dog extends Animal{  
String color="black";  
void printColor(){  
System.out.println(color);
System.out.println(super.color);
}  
}  
class TestSuper1{  
public static void main(String args[]){  
Dog d=new Dog();  
d.printColor();  
}
}
Output:
black
white

2. To Refer method of an immediate parent class:
    The super keyword can also be used to invoke parent class method. It should be used if subclass contains the same method as parent class. In other words, it is used if method is overridden. For example,
class Animal{  
void eat(){System.out.println("eating...");}  
}  
class Dog extends Animal{  
void eat(){System.out.println("eating bread...");}  
void bark(){System.out.println("barking...");}  
void work(){  
super.eat();  
bark();  
}  
}  
class TestSuper2{  
public static void main(String args[]){  
Dog d=new Dog();  
d.work();  
}
}     
Output:
eating...
barking...

3. To Refer constructor of an immediate parent class
    The super keyword can also be used to invoke the parent class constructor. Let's see a simple example:
class Animal{  
Animal(){System.out.println("animal is created");}  
}  
class Dog extends Animal{  
Dog(){  
super();  
System.out.println("dog is created");  
}  
}  
class TestSuper3{  
public static void main(String args[]){  
Dog d=new Dog();  
}
}  
Output:
animal is created
dog is created

Final

    Final keyword is use to restrict the user. 

We can use this keyword with:

  1. Variable
  2. Method
  3. Class
1. With Variable:
    If we use final keyword with variable then the value of that variable can not be change.(It will be the constant). For example,

class Bike9{
final int speedlimit=90;
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}

Output:
Output:Compile Time Error

2. With Method:
    If you make any method as final, you cannot override it. For example,
class Bike{
  final void run(){System.out.println("running");}
}
   
class Honda extends Bike{
   void run(){System.out.println("running safely with 100kmph");}
   
   public static void main(String args[]){
   Honda honda= new Honda();
   honda.run();
   }
}
Output:
Output:Compile Time Error

3. With Class:
    If you make any class as final, you cannot extend it. For example,
final class Bike{} 
 class Honda1 extends Bike{
   void run(){System.out.println("running safely with 100kmph");}
   
   public static void main(String args[]){
   Honda1 honda= new Honda();
   honda.run();
   }
 }
Output:
Output:running...

Comments

Popular posts from this blog

Introduction of Java

 What is Java? Java is an object-oriented programming language developed by Sun Microsystem in 1995. It is a combination of C and C++ features with some essential and additional concepts. Java is suited for both standalone and web application development. Java can run on any hardware OR software environment which make it platform independent. Java Example:                                                                                           class Simple{     public static void main(String args[]){          System.out.println("Welcome In Java World");     } } In above example the 'Simple' is a Class name, public is access specifier , Main is a method with argument, St...

Java Package

 Introduction      A java package is a group of similar types of classes, interfaces and sub-packages. Package in java can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Here, we will have the detailed learning of creating and using user-defined packages. Simple example of java package The package keyword is used to create a package in java. //save as Simple.java   package mypack;   public class Simple{    public static void main(String args[]){       System.out.println("Welcome to package");      }   }   How to access package from another package? There are three ways to access the package from outside the package. 1. import package.*; 2. import package.classname; 3. fully qualified name. 1. Using packagename.*      If you use package.* the...

Java Classes, Object and Methods, Constructors

 Defining a class     A class is a user-defined data type or group of object with a template or blueprint that serves to define its properties. It is a logical entity. Once a class type has been define, we can create " Variables " which are similar to basic type declaration. In java, variables are termed as instance of class, which actual object. The basic form class defination is as follows: class classname [extends superclassname]{     [fields declaration]     [method declaration] } The classname and superclassname are valid java identifiers. The keyword extends indicate that superclassname properties extended into classname. It is a pert of inheritance. (We will discuss inheritance in later part.) Everything inside square brackets is optional that means we can use as per the requirements. For example, class Empty{ } In above example, the Empty class is class name. This class does not have any properties and therefor cannot do anything. A java cla...