0% found this document useful (0 votes)
9 views

Super, Abstract

Uploaded by

Factorian Facts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Super, Abstract

Uploaded by

Factorian Facts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Super Keyword in Java

• The super keyword in Java is a reference variable which is used to refer immediate parent
class object.

• Whenever you create the instance of subclass, an instance of parent class is created implicitly
which is referred by super reference variable.

Usage of Java super Keyword

• super can be used to refer immediate parent class instance variable.

• super can be used to invoke immediate parent class method.

• super() can be used to invoke immediate parent class constructor.


super is used to refer immediate parent class instance variable

class Animal{
String color="white";
}
class Dog extends Animal{ Output
String color="black"; Black
White
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//
prints color of Animal class
} Animal and Dog both classes have a common
} property color. If we print color property, it
class TestSuper1{ will print the color of current class by default.
public static void main(String args[]){ To access the parent property, we need to
Dog d=new Dog(); use super keyword.
d.printColor();
}
}
super can be used to invoke parent class method
class Animal{
void eat(){
System.out.println("eating..."); }
} Output
class Dog extends Animal{ eating...
void eat(){ barking...
System.out.println("eating bread..."); }
void bark(){
System.out.println("barking..."); }
void work(){
super.eat(); Animal and Dog both classes have eat()
bark(); method if we call eat() method from Dog
} class, it will call the eat() method of Dog class
} by default because priority is given to local.
class TestSuper2{
public static void main(String args[]){ To call the parent class method, we need to
Dog d=new Dog(); use super keyword.
d.work();
}
}
super is used to invoke parent class constructor

class Animal{
Animal(){System.out.println("animal is created");}
} Output
class Dog extends Animal{ animal is created
Dog(){ dog is created
super();
System.out.println("dog is created");
}
} Note: super() is added in each class
class TestSuper3{ constructor automatically by compiler if there
public static void main(String args[]){ is no super() or this()
Dog d=new Dog();
}}
super keyword : Example
class Person{
int id; void display(){

String name; System.out.println(id+" "+name+" "+salary);

Person(int id,String name){ }

this.id=id; }

this.name=name; class TestSuper5{

} public static void main(String[] args){

} Emp e1=new Emp(1,"ankit",45000f);

class Emp extends Person{ e1.display();

float salary; }

Emp(int id,String name,float salary){ }

super(id,name);//reusing parent constructor


this.salary=salary; Output:

} 1 ankit 45000
Abstract class in Java

• A class which is declared with the abstract keyword is known as an abstract class in Java.

• It can have abstract and non-abstract methods (method with the body).

Abstraction in Java

• Abstraction is a process of hiding the implementation details and showing only functionality to
the user.

• Another way, it shows only essential things to the user and hides the internal details

There are two ways to achieve abstraction in java


1. Abstract class (0 to 100%)
2. Interface (100%)
Abstract class in Java
Abstract class in Java
Example of Abstract class that has an abstract method
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){
System.out.println("running safely");
} Output:
running safely

public static void main(String args[]){


Bike obj = new Honda4();
obj.run();
}
}
Example of Abstract class that has an abstract method
abstract class Shape{
abstract void draw();
}
//In real scenario, method is called by programmer or user
//In real scenario, implementation is
class TestAbstraction1{
provided by //others i.e. unknown by end
public static void main(String args[]){
user
Shape s=new Circle1();
class Rectangle extends Shape{
s.draw();
void draw(){
}
System.out.println("drawing rectangle");}
}
}
class Circle1 extends Shape{
Output
void draw(){
drawing circle
System.out.println("drawing circle");}
}
Example of Abstract class that has an abstract method
class TestBank{
abstract class Bank{
public static void main(String args[]){
abstract int getRateOfInterest();
Bank b;
}
b=new SBI();
class SBI extends Bank{
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+"
int getRateOfInterest(){
%");
return 7;
b=new PNB();
}
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+"
}
%");
class PNB extends Bank{
}
int getRateOfInterest(){
}
return 8;
Output
}
Rate of Interest is: 7 %
}
Rate of Interest is: 8 %
Abstract class having constructor, data member and methods
//Example of an abstract class that has abstract and non-abstract
methods

abstract class Bike{


Bike(){ //Creating a Test class which calls abstract and non-
System.out.println("bike is created");} abstract methods
abstract void run(); class TestAbstraction2{
void changeGear(){ public static void main(String args[]){
System.out.println("gear changed"); Bike obj = new Honda();
} obj.run();
} obj.changeGear();
//Creating a Child class which inherits Abstract }
class }
class Honda extends Bike{
void run(){
System.out.println("running safely..");
}
Another real scenario of abstract class
interface A{ class Test5{
void a(); public static void main(String args[]){
void b(); A a=new M();
void c(); a.a();
void d(); a.b();
} a.c();
abstract class B implements A{ a.d();
public void c(){System.out.println("I am c");} }
} }
class M extends B{
public void a(){System.out.println("I am a");} Output:I am a
public void b(){System.out.println("I am b");} I am b
public void d(){System.out.println("I am d");} I am c
} I am d
final Keyword in Java

• The final keyword in java is used to restrict the user. The java final keyword can be

used in many context. Final can be:

1.variable

2.method

3.class
1) Java final variable
If you make any variable as final, you cannot change the value of final
ariable(It will be constant).

class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
Output:Compile Time Error
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}//end of class
2) Java final method
If you make any method as final, you cannot override it.

class Bike{
final void run(){System.out.println("running");}
}

class Honda extends Bike{


void run(){System.out.println("running safely
with 100kmph");} Output:Compile Time Error

public static void main(String args[]){


Honda honda= new Honda();
honda.run();
}
}
3) Java final class
If you make any class as final, you cannot extend it.

final class Bike{}

class Honda1 extends Bike{


void run(){System.out.println("running safely
with 100kmph");}
Output:Compile Time Error
public static void main(String args[]){
Honda1 honda= new Honda1();
honda.run();
}
}
Is final method inherited?
final method is inherited but you cannot override it.

class Bike{
final void run(){
System.out.println("running...");
}
}
Output:running...
class Honda2 extends Bike{
public static void main(String args[]){
new Honda2().run();
}
}
Static Binding and Dynamic Binding

• Connecting a method call to the method body is known as binding.

There are two types of binding

• Static Binding (also known as Early Binding).

• Dynamic Binding (also known as Late Binding).


1) variables have a type

• Each variable has a type, it may be primitive and non-primitive.

int data=30;
• Here data variable is a type of int.
2) References have a type

class Dog{
public static void main(String args[]){
Dog d1;//Here d1 is a type of Dog
}
}
3) Objects have a type
An object is an instance of particular java class,but it is also an instance of its superclass.
class Animal{}

class Dog extends Animal{


public static void main(String args[]){
Dog d1=new Dog();
}
}

Here d1 is an instance of Dog class, but it is also an instance of Animal.


static binding
When type of the object is determined at compiled time(by the compiler), it is known as static
binding.
If there is any private, final or static method in a class, there is static binding.

class Dog{
private void eat(){
System.out.println("dog is eating...");
}
Output:running...
public static void main(String args[]){
Dog d1=new Dog();
d1.eat();
}
}
Dynamic binding
When type of the object is determined at run-time, it is known
as dynamic binding.
class Animal{
void eat(){System.out.println("animal is
eating...");}
}

class Dog extends Animal{


void eat(){System.out.println("dog is eating...");
}
Output:dog is eating...
public static void main(String args[]){
Animal a=new Dog();
a.eat();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy