0% found this document useful (0 votes)
5 views17 pages

Abstraction

The document explains the super keyword in Java, which is used to refer to the immediate parent class's instance variables, methods, and constructors. It also covers the concepts of abstraction, detailing how it can be achieved through abstract classes and interfaces, along with examples. Additionally, it highlights the differences between abstract classes and interfaces in Java.

Uploaded by

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

Abstraction

The document explains the super keyword in Java, which is used to refer to the immediate parent class's instance variables, methods, and constructors. It also covers the concepts of abstraction, detailing how it can be achieved through abstract classes and interfaces, along with examples. Additionally, it highlights the differences between abstract classes and interfaces in Java.

Uploaded by

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

OOPs with Java

Super keyword, Abstraction


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.
1) to refer immediate parent class instance variable.

● 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.

class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal class
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog(); Output:
d.printColor(); black
}} white
2) super can be used to invoke parent class method
● 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.
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[]){ Output:
Dog d=new Dog(); eating…
d.work(); barking…
}}
3) super is used to invoke parent class constructor.
● 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
class Person{
int id;
String name;
Person(int id,String name){
this.id=id;
this.name=name;
}
}
class Emp extends Person{
float salary;
Emp(int id,String name,float salary){
super(id,name);//reusing parent constructor
this.salary=salary;
}
void display(){System.out.println(id+" "+name+" "+salary);}
}
class TestSuper5{
public static void main(String[] args){
Emp e1=new Emp(1,"ankit",45000);
Output:
e1.display(); 1 ankit 45000
}}
ABSTRACTION

● Abstraction is a process of hiding the implementation details and showing only


functionality to the user.

● Another way, it shows only important things to the user and hides the internal details
for example sending sms, you just type the text and send the message. You don't know
the internal processing about the message delivery.

● Abstraction lets you focus on what the object does instead of how it does it.
Ways to achieve Abstraction

● There are two ways to achieve abstraction in java

○ Abstract class (0 to 100%)

○ Interface (100%)
● Abstract class in Java

○ A class that is declared as abstract is known as abstract class. It needs to be


extended and its method implemented. It cannot be instantiated.

○ Example abstract class

■ abstract class A{ }

● Abstract method

○ A method that is declared as abstract and does not have implementation is


known as abstract method.

○ Example abstract method

■ abstract void printStatus();//no body and abstract


Example of abstract class that has abstract method
● In this example, Bike the abstract class that contains only one abstract method run. It
implementation is provided by the Honda class.

abstract class Bike{


abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely..");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
abstract class Shape{
abstract void draw();
}
//In real scenario, implementation is provided by others i.e. unknown by end user
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
}
//In real scenario, method is called by programmer or user
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();//In real scenario, object is provided through method e.g. getShape() method
s.draw();
}
}
Interface in Java

● An interface in java is a blueprint of a class. It has static constants and abstract


methods.

● The interface in java is a mechanism to achieve abstraction. There can be only abstract
methods in the java interface not method body. It is used to achieve abstraction and
multiple inheritance in Java.

● Java Interface also represents IS-A relationship.

● It cannot be instantiated just like abstract class.


Why use Java interface?

● There are mainly three reasons to use interface. They are given below.

○ It is used to achieve abstraction.

○ By interface, we can support the functionality of multiple inheritance.

○ It can be used to achieve loose coupling


Java Interface Example

● In this example, Printable interface has only one method, its implementation is
provided in the A class.

interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}

public static void main(String args[]){


A6 obj = new A6();
obj.print();
}
}
Differences between abstract class and interface
Abstract class Interface

1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can have default and
static methods also.

2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.

3) Abstract class can have final, non-final, static and non-static Interface has only static and final variables.
variables.

4) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.

5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.

6) An abstract class can extend another Java class and implement An interface can extend another Java interface only.
multiple Java interfaces.

7) An abstract class can be extended using keyword "extends". An interface can be implemented using keyword "implements".

8) A Java abstract class can have class members like private, protected, Members of a Java interface are public by default.
etc.

9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
interface A{
void a(); void b();
void c(); void d();
}
abstract class B implements A{
public void c(){System.out.println("I am c");}
}
class M extends B{
public void a(){System.out.println("I am a");}
public void b(){System.out.println("I am b");}
public void d(){System.out.println("I am d");}
}
class Test5{
public static void main(String args[]){
A a=new M();
a.a();
a.b();
a.c();
a.d();
}}

You might also like