Vous êtes sur la page 1sur 4

Features of java: Simple.Secure.Portable.Object-oriented.Robust.Multithreaded Architecture-neutral.Interpreted.High performance.Distributed.Dynamic .

Wap to add and delete stack:


import java.util.*; public class StackDemo { static void showpush(Stack st, int a) { st.push(new Integer(a)); System.out.println("push(" + a + ")"); System.out.println("stack: " + st); } static void showpop(Stack st) { System.out.print("pop -> "); Integer a = (Integer) st.pop(); System.out.println(a); System.out.println("stack: " + st); } public static void main(String args[]) { Stack st = new Stack(); System.out.println("stack: " + st); showpush(st, 42); showpush(st, 66); showpush(st, 99); showpop(st); showpop(st); showpop(st); try { showpop(st); } catch (EmptyStackException e) { System.out.println("empty stack"); } } }

Overloading Happening within the same class. Method signature should not be same. It happen at time of compliance or we can say overloading is the early binding or static binding. Method can have any return type.

Overriding Happening between super class and sub class. Method signature should be same. It happen on time of run time or we can say overriding is dynamic binding or let binding. Method return type must be same as super class method Method must have same or wide access level than super class method access level.

Method can have any access level.

Overriding means we implement a method in the subclass that has the same signature as one in the parent class. e.g // Parent class public class PClass{ public void func(){ } } // Child class public class CClass{ public void func(){ // overriden method that has the same signature } } Overloading: we only cahnge the parameters // Parent class public class PClass{ public void func(int a){ } } // Child class public class CClass{ public void func(int a, int b){ /* overriden method that has the same signature but different arguments */ } } *****************************************************************************************

Interface
An interface differs from an abstract class because an interface is not a class. An interface is essentially a type that can be satisfied by any class that implements the interface. Any class that implements an interface must satisfy 2 conditions: 1. It must have the phrase "implements Interface_Name" at the beginning of the class definiton. 2. It must implement all of the method headings listed in the interface definition

3. public interface Dog 4. { 5. public boolean Barks(); 6. 7. public boolean isGoldenRetriever();

}public class SomeClass implements Dog

{ public boolean Barks{ // method definition here } public boolean isGoldenRetriever{ // method definition here } }

Abstract class
Abstract clases are meant to be inherited from, and when one class inherits from another it means that there is a strong relationship between the 2 classes. For instance, if we have an abstract base class called "Canine", any deriving classshould be an animal that belongs to the Canine family (like a Dog or a Wolf) Ex:

public abstract class Canine { public abstract boolean Dog(); public abstract boolean Wolf(); } public class Should extends Canine { public boolean Dog { // method definition here } Public boolean Wolf{

// method definition here }

Vous aimerez peut-être aussi