• No se han encontrado resultados

SECCIÓN 8ª.- FECHA Y HORARIOS DE LAS CONFRONTACIONES

There are following oops Concepts :

» Abstraction » Encapsulation » Inheritance » Polymorphism

To describe object, there are two important rules in java : » Properties , also called Variables.

» Operations , also called Methods.

Abstraction : Providing necessary operations and properties of certain object by hiding internal details is called Abstraction.

Encapsulation : Placing the data and operations which are going to operate on data in a single entity is called Encapsulation.

Inheritance : Creating a new class by using the functionality of existing class.This new class is called subclass or child class or derived class .The existing

class is called super class or parent class or base class. Main use of interitance is reusability.

Polymorphism :

A process of behaving one form differently in different situation is called polymorphism.It is to create multiple definitions for operations.

These are two types :

1. Compile time polymorphism : (early binding)

This can be achieved with the help of method overloading. 2. Runtime polymorphism : (late binding)

This can be achieved with the help of method overriding.

Objects & Classs :

Class is a logical entity which is a template or plan or application form for an object.Object is a physical entity which is instance of the class.

Declaring the class : syntax:

class classname {

Datatype variable1,variable2,? return type method name(parameters) {

--- --- }

}

» Variables and methods are called as members of the class. Ex: class Student { int sno=3; String sname="sri"; Void display() { System.out.println("Student id no is:"+sno); System.out.println("Student name is:"+sname); }

}

Creating object : Syntax:

class-name reference variable = new class-name(); Ex: Student s= new Student();

» One object will be created for Student class.

» In the above example 's' is the reference variable . » Student () is the constructor of Student class. Calling the members of the class :

Syntax:

Object-name. member name Ex: s.display(); Example: class Student { int sno=3; String sname="sri"; Void display() { System.out.println("Student id no is:"+sno); System.out.println("Student name is:"+sname); }

}

class Demo {

public static void main(String as[]) {

Student s= new Student(); s.display(); } } s- is a reference variable.

OOPS IN JAVA-II

Abstract class :

Example :

abstract class Animal {

abstract void eat(); void show()

{

System.out.println("This is Animal show"); }

}

class Demo {

public static void main(String as[]) {

Animal a=new Animal(); //not allowed. a.show();

} }

Explanation :

» When you are unable to implement the method (Without body) declare the method as abstract. » When you declare one (or) more methods as abstract you must declare the class also abstract. » When class is abstract it can't be instantiated.

» In an abstract class we can have both abstract method and concrete methods. » In abstract class we can have only concrete methods also.

» When class is abstract we should write a subclass for that (or)subclass is responsible to implement the abstract class.

» With subclass object only we can call the subclass methods as well as abstract class concrete methods.

» When you extend any abstract class we should override all the abstract methods in subclass, otherwise declare the subclass as abstract.

Polymorphism :

» There are two types of polymorphism.

1.Compile time polymorphism (or) static polymorphism 2.Runtime polymorphism (or) Dynamic polymorphism

» We can achieve Compile time polymorphism with method overloading. » We can achieve Runtime polymorphism using method overriding.

» At Compile time, compiler verifies method overloading rules and to invoke these methods it won't use object. It will use parameters of the method.

» At Run time JVM decides which method it has to invoke (super class method or subclass method) based on object which you are using to call the method.

Note :-

» We can assign subclass object to super class reference variable but we can't assign super class object to subclass reference variable.

» When class is abstract we can't create object for that but we can declare the reference variable. » If we want to achieve the Runtime polymorphism, we must declare super class as abstract.

Interfaces :

Need for Interfaces :

The benefits of using interfaces are much the same as the benefits of using abstract

classes.Interfaces provide a means to define the protocols for a class without bothering about the implementation details.This seemingly simple benefit can make large projects much easier to manage.once interfaces have been designed, the class development can take place without bothering about communication among classes.

Another important use of interfaces is the capacity for a class to implement multiple interfaces.The major difference between inheriting multipleinterfaces and true multiple inheritance is that the interface approach enables you to inherit only method

descriptions,implementations.If a classimplements multiple interfaces, that class must provide all the functionality for the methods defined in the interfaces.

» Interface is a fully abstracted class.

» Interface can have only final static variables and public abstract methods. Syntax :

interface interface-name {

--- ---

return type method name (argument1,argument2,..); ---

}

Difference between abstract class and interface :

Abstract class interface

1.Abstract class contains constants, variables,

constructors,concrete methods and abstract methods.

1.Interface contains constants and public abstract methods.

2.Use extends keyword to extend the abstract class. 2.Use 'implements' keyword to implement the interface.

3.Abstract class doesn't support multiple inheritance i.e. after extends keyword more than one class name is not allowed.

3.Interface support multiple inheritance i.e. after implements keyword more than one class name is allowed.

4.When you are writing abstract methods in abstract class we should write abstract keyword.

4.When you are writing abstract methods in interface, no need to write abstract keyword.

5.If you want to declare final static variable and public abstract methods in abstract class, we should declare them explicitly.

5.By default all the variables are final and static and all the methods are public and abstract.

Documento similar