Core Java(Part-7)

Content
.Typecasting
.Abstract
.final
.this keyword
.super keyword
Typecasting
Forcing one type to behave like other type.
You convert one datatype to another.
You have a big glass of water,
but your small bottle can hold only a little water.
So, you pour carefully — that's typecasting!
You can perform typecasting in two ways
.Widening
Converting value from lower datatype to higher data type, in this there is no data loss. You shift the value of int variable into long variable which can easily happen without any disturbance or error because the size of long datatype is bigger than the int datatype. int datatype is of 4byte, long datatype is of 8 byte so you can easily transfer.
public class Widening {
public static void main(String args[]){
int a=10;
long b=a;//widening
System.out.println(b+"Widening");
}
}
.Narrowing
Converting value from higher datatype to higher datatype, the possibility of data loss in narrowing is possible. Shifting the value of long datatype variable to int datatype variable, so the possibility of data loss in this typecasting is possible you can say.
public class Narrowing {
public static void main(String args[]){
long a=10;
int b=(int)a;//narrowing
System.out.println(b+"Narrowing");
}
}
When you try to run above program without narrowing then it throws a error this
java: incompatible types: possible lossy conversion from long to int
abstract
.A class which is declared using abstract keyword known as abstract class
.An abstract class may or may not have abstract method
.We cannot create object of abstract class.
.It is used to achieve abstraction but it doesn’t provide 100% abstraction because it can have concrete methods
Note:-An abstract class have abstract and non abstract methods
Syntax:-
abstract class class_name{}
Abstract Method
Method that are declared without any body within abstract class are called abstract method. The method body will be defined by it’s subclass
Abstract method can never be final and static
Any class that extent and abstract class must implement all the abstract methods
final
It is a keyword or modifier which can be used at variable, method or classes
Syntax:-
final int a=10;
final variable
When you use final keyword with datatype then you can’t again reassign the value to that variable, after that it behave like a constant you can say. When you try to reassign it shows warning.
package finalDemo;
public class FinalDemo {
public static void main(String args[]){
int a=10;
a=20;
System.out.println(a);
final int b=10;//
//b=20; warning
System.out.println(b);
}
}
final method
You can’t override the final method of Parent class in Child class.
package finalDemo;
class Parent{
final public void eat(){
System.out.println("Parent eat");
}
}
class Child extends Parent{
Overriden is not possible
}
public class FinalMethod {
public static void main(String args[]){
Parent p=new Parent();
Child c=new Child();
p.eat();
c.eat();
}
}
when you try to override it, it throws a error that
java: eat() in finalDemo.Child cannot override eat() in finalDemo.Parent
overridden method is final
final class
You can’t inherit the final class. Inheritance is not possible in final class
package finalDemo;
final class Parent1{
public void eat(){
System.out.println("Hello");
}
}
class Child1 extends Parent1{
public void eat(){
System.out.println("World");
}
}
public class FinalClass {
public static void main(String args[]){
Child c=new Child();
c.eat();
}
}
When you try to extends final Parent1 class into Child1 class then throws an error that
java: cannot inherit from final finalDemo.Parent1
this keyword
.In java this is a keyword which we used in method or in constructor.
.this keyword refers to current object in a method or constructor
package inheritance;
class Animal{
Animal(){
}
Animal(String a){
System.out.println(a);
}
public void eat(String s){
System.out.println(s);
System.out.println("Animal eat");
}
}
class CarnivoreAnimal extends Animal{
CarnivoreAnimal(String s){
System.out.println(s);
}
CarnivoreAnimal(){
this("world");
}
}
public class Inheritance {
public static void main(String args[]){
CarnivoreAnimal wa=new CarnivoreAnimal();
Animal animal=new Animal();
animal.eat("hello");
}
}
Output
Animal Zero Parameterized Constructor
CarnivoreAnimal Parameterized Constructor
Animal Zero Parameterized Constructor
Animal Parameterized Constructor
In above program
Flow of main method()
CarnivoreAnimal wa = new CarnivoreAnimal();
Calls
CarnivoreAnimal()
→ which callsthis("CarnivoreAnimal Parameterized Constructor")
Prints:
CarnivoreAnimal Parameterized Constructor
(Java automatically calls
super()
→Animal()
constructor), so prints:Animal Zero Parameterized Constructor
Animal animal = new Animal();
Calls
Animal()
constructor directly.Prints:
Animal Zero Parameterized Constructor
animal.eat
("Animal Parameterized Constructor");
Prints:
Animal Parameterized Constructor
this()
= Call own constructor
super()
= Call parent constructor
If not written, Java automatically puts super()
as first line.
Constructor chaining happens.
super keyword
super keyword is used to call the Parent class constructor
package inheritance;
class Parent{
Parent(){
System.out.println("Parent Zero Parametrized Constructor");
}
Parent(String a){
}
public void eat(){
System.out.println("Parents eat");
}
}
class Child extends Parent{
Child(){
super();
System.out.println("Child Zero Parametrized Constructor");
}
Child(String a){
}
}
public class Inheritance2 {
public static void main(String args[]){
Child c=new Child();
Parent p=new Parent();
}
}
Output
Parent Zero Parametrized Constructor
Child Zero Parametrized Constructor
Parent Zero Parametrized Constructor
In Above program
main() method working
Child c = new Child();
Child()
runs.super();
callsParent()
first → prints:Parent Zero Parametrized Constructor
Then
Child()
prints:Child Zero Parametrized Constructor
Parent p = new Parent();
Calls
Parent()
constructor → prints:Parent Zero Parametrized Constructor
Okay that’s it for today. See you in my next Java Core Series. Best of luck for your journey. Thankyou for reading my article
Subscribe to my newsletter
Read articles from Khelendra Kumar Thakur directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
