Use of Abstract Keyword in Java like Abstract Class

Hi friends, today we will discuss another important keyword of Java called Abstract keyword. Note down this important points of the Abstract keyword

  1. Abstract class acts as a base class for other classes of same type.
  2. Abstract class can’t be instantiated.
  3. There can be any abstract class without any abstract method but vice versa is not possible.
  4. If we are extending a class from an abstract class then we have to provide definition of all the abstract method in the first concrete class (concrete class means not abstract class).
  5. Abstract class can have constructors.
  6. Abstract class can have main method also.

Let’s do some practical on Abstract keyword. It will clear our doubts. After all learning through practical implementations will make our concept stronger.

Program 1

class abs1
{
void disp();
}

Output

This program will give a compile time error because it is a simple class program where we need to give definition to function of the class. Here we didn’t define the function of the class. Hence we get compile time error.

Program 2

class abs2
{
abstract void disp();
}

Output

Compile time error.

Reason :- abs 2 should be declared abstract. As I said before, if we declare any method inside the class as abstract then we have to declare the same class as abstract otherwise it will give a compile time error.

Program 3

abstract class abs3
{
abstract void disp();
}

Output

It will compile successfully.

Reason: – Since we have declared both method and class as abstract so there won’t be a problem.

Program 4

abstract class abs4
{
static void show()
{
System.out.println(“Hello”);
}
public static void main(String[] args)
{
show();
}
}

Output

Hello

Reason: – It will print the Hello statement. Here we have used a static keyword with the function, so we didn’t create instance of class. Now check out the program 5, it will make you things more clear.

Program 5

abstract class abs5
{
void show()
{
System.out.println(“Hello”);
}
public static void main(String[] args)
{
new abs5.show();
}
}

Output

Compile time error.

Reason: – We tried to instantiate and its clearly written in the above statement of abstract that Abstract class can’t be instantiated. This program 5 is almost same to program 4. Here we just removed the static keyword. Since the method or function is not static now, so we will need to instantiate to call the method but we are not allowed to instantiate in abstract class. So we get compiler time error.

Program 6

abstract class abs7
{
abstract void disp();
}

class check extends abs7
{
public static void main(String[] ar)
{
new check().disp();
}
}

Output

Compile time error.

Reason: – Here we have created two class check and abs7. abs7 is the base class and abstract class. We are inheriting the abstract class here but we are not defining the abstract method in the derived class. Since we didn’t define the abstract class in the in the derive class it caused a compiler error. If you read the above statement of abstract class its clear written that “If we are extending a class from an abstract class then we have to provide definition of all the abstract method in the first concrete class (concrete class means not abstract class).

Note: – I will update this post more later.



Share this Post[?]

        

Use of Super Keyword in Java

Ok today we will try to discuss the super keyword of Java. What is the super keyword in Java?, What is the use of super Keyword in Java?, How to practically implement super keyword in Java?, we will discuss all this things. So first note down these important points regarding super keyword in Java.

  1. Super keyword is used to call immediate parent.
  2. Super keyword can be used with instance members i.e., instance variables and instance methods.
  3. Super keyword can be used within constructor to call the constructor of parent class.

OK now let’s practically implement this points of super keyword.

Check out the difference between program 1 and 2. Here program 2 proofs our firstĀ  statement of Super keyword in Java.

Program 1

class base
{
int a = 100;
}

class sup1 extends base
{
int a = 200;
void show()
{
System.out.println(a);
System.out.println(a);
}
public static void main(String[] args)
{
new sup1().show();
}
}

Output : -

200

200

Now check out the program 2 and try to figure out the main difference.

Program 2

class base
{
int a = 100;
}

class sup2 extends base
{
int a = 200;
void show()
{
System.out.println(super.a);
System.out.println(a);
}
public static void main(String[] args)
{
new sup2().show();
}
}

Output

100

200

In the program 1, the output was only of the derived class. It couldn’t print the variable of base class or parent class. But in the program 2, we used a super keyword with the variable a while printing its output and instead of printing the value of variable a of derived, it printed the value of variable a of base class. So it proofs that Super keyword is used to call immediate parent.

OK now check out the difference between program 3 and program 4

Program 3

class base
{
int a = 100;
void show()
{
System.out.println(a);
}
}

class sup3 extends base
{
int a = 200;
void show()
{
System.out.println(a);
}
public static void main(String[] args)
{
new sup3().show();
}
}

Output

200

Here the output is 200. When we called the show function, the show function of derived class was called. But what should we do if we want to call the show function of the parent class. Check out the program 4 for solution.

Program 4

class base
{
int a = 100;
void show()
{
System.out.println(a);
}
}

class sup4 extends base
{
int a = 200;
void show()
{
super.show();
System.out.println(a);
}
public static void main(String[] args)
{
new sup4().show();
}
}

Output

100

200

Here we are getting two output 100 and 200. When the show function of derived class is invoke, it first calls the show function of parent class because inside the show function of derived class we called the show function of parent class by putting the super keyword before the function name.

Ok here’s a program for you. You need to predict the output of this program

class base
{
int a=100;
void show()
{
System.out.println(a);
}
}

class sup5 extends base
{
int a=200;
void show()
{
System.out.println(a);
super.show();
}
public static void main(String[] args)
{
new sup5().show();
}
}

So tell us, what will be the output? So now I think you too know what is super keyword in Java? How and where the super keyword is used in Java? So friends, if you find or face any difficulty in learning this super keyword of Java then let me know.


Share this Post[?]
        

Next Page »