Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Rima Patel
So far we have covered
1. Class
2. Object
3. Inheritance
4. Polymorphism
What is abstraction in Java?
Bank ATM
What is the difference between
Abstraction & Encapsulation?
Abstraction lets you focus on what the object does instead of
how it does, while Encapsulation means hiding the internal
details of how an object works. ...
Abstraction hides unwanted data and provides the relevant
data.
//Abstract method
abstract void printStatus();
How normal method different from
abstract method?
Why?
• To use an abstract class, you have to inherit it from
another class, provide implementations to the
abstract methods in it.
• If you inherit an abstract class, you have to provide
implementations to all the abstract methods in it.
Example-3 abstract class C extends B
{
abstract class A void m2()
{ { S.O.P.(“m2 method”);}
abstract void m1(); }
abstract void m2(); class D extends C
abstract void m3(); {
} void m3()
{ S.O.P.(“m3 method”);}
abstract class B extends A P.S.V.M()
{ {
D d = new D();
void m1()
d.m1(); d.m2(); d.m3();
{ S.O.P.(“m1 method”);} }
} Will it compile successfully?
YES
Is it possible to declare main method in
abstract class?
abstract class Test
{
P.S.V.M()
{
S.O.P(“Hello abstract class”);
}
}
Compile & run successfully
3.
Abstract class constructor
Its possible
abstract class Test
{
Test()
{
System.out.println("abstarct class constructor");
}
public static void main(String args[])
{
System.out.println("Hello abstract class");
}
}
Inside the abstract class, constructor
declaration is possible or not?
abstract class Test
{
Test()
{
System.out.println("abstarct class constructor");
}
public static void main(String args[])
{
Teat t=new Test();
System.out.println("Hello abstract class");
}
}
}
Duplicate abstract method Example
abstract class One {
abstract void m1();
void m1(){
System.out.println("non abstract method" ); Error-
}}
method
public class MyClass extends One { m1() is
void m1() already
{ System.out.println("abstract class method" );
} defined
public static void main(String args[]) { in class
System.out.println("My class"); One
MyClass m = new MyClass();
m.m1();
}}
Will it compile or not?
What is the output here
abstract class Bike{ No error
abstract void run();
} Execute
successfully
class Honda extends Bike{
void run()
{
System.out.println("running safely..");
}
public static void main(String args[]){
Bike obj = new Honda(); // is there any error here
obj.run();
}
}
What is the output here
abstract class Bank{
abstract int getRateOfInterest();
}
class SBI extends Bank{
int getRateOfInterest(){return 7;}
}
class PNB extends Bank{
int getRateOfInterest(){return 8;}
}
class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
}}
What is the output here
abstract class Bike{
Bike() { System.out.println("bike is created");}
abstract void run();
void changeGear() { System.out.println("gear changed");}
}
YES
//Interface declaration: by first user
interface Drawable{
void draw();
}
//Implementation: by second user
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class Circle implements Drawable{
public void draw(){System.out.println("drawing circle");}
}
//Using interface: by third user
class TestInterface{
public static void main(String args[]){
Drawable d=new Circle();
d.draw();
}}
How to achieve Multiple inheritance in
Java by interface?
What is the output here?
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
YES
Example
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class ExtendInterface implements Showable{
}}
• The reason we have default methods in
interfaces is to allow the developers to add
new methods to the interfaces without
affecting the classes that implements
these interfaces.
Java 8: Static Method in Interface
• These static method will act as helper
methods.
• These methods are the parts of interface not
belongs to implementation class objects.
interface Drawable{
void draw();
static int cube(int x){return x*x*x;}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class TestInterfaceStatic{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
System.out.println(Drawable.cube(3));
}}
Example
interface StaticInterface{
static void print(String str){
System.out.println("Static method of interface:"+str);
}
}
class Demo implements StaticInterface{
public static void main(String[] args){
StaticInterface.print("Java 8")
}
}
Abstract class Interface
1) Abstract class can have abstract and Interface can have only abstract methods. Since Java
non-abstractmethods. 8, it can have default and static methods also.
3) Abstract class can have final, non-final, static Interface has only static and final variables.
and non-static variables.
4) Abstract class can provide the implementation of Interface can't provide the implementation of
interface. abstract class.
5) The abstract keyword is used to declare abstract The interface keyword is used to declare interface.
class.
6) An abstract class can extend another Java class An interface can extend another Java interface only.
and implement multiple Java interfaces.
7) An abstract class can be extended using keyword An interface can be implemented using keyword
"extends". "implements".
8) A Java abstract class can have class members like Members of a Java interface are public by default.
private, protected, etc.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
• //Creating interface that has 4 methods
• interface A{
• void a();//bydefault, public and abstract
• void b();
• void c();
• void d();
• }
•
• //Creating abstract class that provides the implementation of one method of A interface
• abstract class B implements A{
• public void c(){System.out.println("I am C");}
• }
•
• //Creating subclass of abstract class, now we need to provide the implementation of rest of the methods
• 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");}
• }
•
• //Creating a test class that calls the methods of A interface
• class Test5{
• public static void main(String args[]){
• A a=new M();
• a.a();
• a.b();
• a.c();
• a.d();
• }}
• loose coupling and tight coupling definition
Loose coupling means reducing dependencies of a class that
use different class directly. Tight coupling means classes and
objects are dependent on one another.
• https://www.interviewsansar.com/2018/03/24/loose-couplin
g-and-tight-coupling-in-java/
Inner Class or nested inner class
Example (nested inner class)
class Outer {
class Inner {
public void show() {
System.out.println("In a nested class method");
}
}
}
class Main {
public static void main(String[] args) {
Outer.Inner in = new Outer().new Inner();
in.show();
}
}
Will it compile or not?
class Outer {
void outerMethod() {
System.out.println("inside outerMethod");
}
class Inner {
public static void main(String[] args){
System.out.println("inside inner class Method");
}
}
}
Error illegal static declaration in inner class
Outer.Inner public static void main(String[]
args) modifier ‘static’ is only allowed in
constant variable declaration
So we can’t have static method in a
nested inner class
{
// data members and methods
public void test_method()
{
........ ........
}
};
/* AnonymousClassDemo.java */
class Dog
{
public void someDog()
{
System.out.println("Classic Dog");
}
}
Anonymous inner class example using
abstract class
abstract class Person{
abstract void eat();
}
class TestAnonymousInner{
public static void main(String args[]){
Person p=new Person(){
void eat(){System.out.println("nice fruits");}
};
p.eat();
}
}
How many class file will be generated
after compilation?
1. Person.class
2. TestAnonymousInner$1.class
3. TestAnonymousInner.class
interface printable{
void print();
interface MessagePrintable{
void msg();
}
}
Example
interface Showable{
void show();
interface Message{
void msg();
}
}
class TestNestedInterface implements Showable.Message{
public void msg(){
System.out.println("Hello nested interface");}
YES
Example
class A{
interface Message{
void msg();
}
}
Adapter Class
Example
Interface It1 Class X implements It1
{ void m1(); { void m1(){}
void m2(); void m2(){}
void m3(); void m3(){}
void m4(); void m4(){}
void m5(); void m5(){}
void m6(); void m6(){}
void m7(); void m7(){}
void m8(); void m8(){}
void m9(); void m9(){}
void m10(); void m10(){}
} }
NO
Example
public interface sample
{
int a=10;
sample(){
}
} Error description:
Interfaces cannot have
constructors.
Reason
• Interfaces in Java don’t have constructor
because all data members in interfaces are
public static final by default, they are
constants(assign values at the time of
declaration) .There are no data members in
interfaces to initialize them through
constructor.