Features of OOP: - Object - Class - Inheritance - Polymorphism - Abstraction - Encapsulation
Features of OOP: - Object - Class - Inheritance - Polymorphism - Abstraction - Encapsulation
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
Features of Java
Simple
Object-Oriented
Platform independent
Secured
Robust
Portable
Dynamic
Interpreted
High Performance
Multithreaded
Distributed
Java vs C++
C++ supports pointers whereas Java does not pointers
Java is platform independent language but c++ is depends upon
operating system machine.
Java uses compiler and interpreter both but in c++ their is only compiler
C++ supports operator overloading and multiple inheritance but java
does not.
Java does is a similar to C++ but not have all the complicated aspects of
C++ (ex: Pointers, templates, unions, operator overloading, structures
etc..)
Thread support is in Java but not in C++.
Internet support is in Java but not in C++
Java does not support header file, include library files just like C++ .
Java use import to include different Classes and methods.
Polymorphism in Java
Polymorphism in java is a concept by which we can perform a single
action by different ways. The word "poly" means many and "morphs"
means forms. So polymorphism means many forms.
class Calculate
{
static int a=3;
static int b;
static void cube(int x)
{
System.out.println(x);
System.out.println(a);
System.out.println(b);
}
Static
{
System.out.println(output is);
b=a*4;
}
public static void main(String args[])
{
cube(5);
}
Can we execute a program without main()
method?
class A3
{
static
{
System.out.println("static block is invoked");
System.exit();
}
}
Java Package
class Employee
{
float salary=40000;
}
class Programmer extends Employee
{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
Types of inheritance in java
Types of inheritance in java
Why multiple inheritance is not
supported in java?
Consider a scenario where A, B and C are three
classes. The C class inherits A and B classes. If A
and B classes have same method and you call it
from child class object, there will be ambiguity
to call method of A or B class.
Method Overriding in Java
class Bike
{
final void run()
{
System.out.println("running");
}
}
class Honda extends Bike
{
void run()
{
System.out.println("running safely with 100kmph");
}
public static void main(String args[])
{
Honda honda= new Honda();
honda.run();
}
}
Java final class
If you make any class as final, you cannot extend it.
A method that is declared as abstract and does not have implementation is known as abstract
method.
But, if a class have at least one abstract method, then the class must be declared abstract.
If you inherit an abstract class you have to provide implementations to all the abstract methods
in it.
Example
abstract class A
{
abstract void callme();
}
class B extends A
{
void callme()
{
System.out.println("this is callme.");
}
public static void main(String[] args)
{
B b=new B();
b.callme();
}
}
Abstraction using abstract class
abstract class Vehicle
{
public abstract void engine();
}
public class Car extends Vehicle
{
public void engine()
{
System.out.println("Car engine"); //car engine implementation
}
public static void main(String[] args)
{
Car v = new Car();
v.engine();
}
}
Java Interface
An interface in java is a blueprint of a class. It has static constants and abstract methods only.
Writing an interface is similar to writing a class. But a class describes the attributes and
behaviours of an object. And an interface contains behaviours that a class implements.
Java Interface
An interface is different from a class in several ways,:
An interface cannot contain instance fields. The only fields that can appear in an interface must
be declared both static and final.
A class can extend only one class, but implement many interfaces.
An interface can extend another interface, similarly to the way that a class can extend another
class.
Java Interface Example
interface printable
{
void print();
}