Sunteți pe pagina 1din 23

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING


Encapsulation Inheritance Polymorphism

INHERITANCE
Super class and Subclass

Using inheritance you can create a general class that trait comments to related items. This class then can be inherited by the other more specific classes each adding things unique to it.
A class that is inherited is called a super class. The class that does the inheritance is called the subclass. Therefore a subclass is a specialized version of a super class.

INHERITANCE

A subclass is a class that inherits attributes and methods from superclass.

ADVANTAGES OF INHERITANCE

Reduce redundancy code redundancy is unnecessary replication of code, inheritance helps to reduce this. Easy maintenance code resides in one place (Super class). Any change made at the super class will automatically change the behavior of the subclass. Extend the functionality of the existing classes by adding more methods to the subclass.

IMPLEMENTING INHERITANCE IN JAVA


The extends keyword is used to derive a class from a superclass. Examples:
public class Cat extends Animal{} public class Dog extends Animal {}

IMPLEMENTING INHERITANCE IN JAVA


class Animal { public void eat() { System.out.println(eating); } } class Cat extends Animal{ public void fish() { System.out.println(fish); } public static void main(String args[]) Cat obj = new Cat(); Obj.eat(); //Inheritance Obj.fish(); //Own method } }

IMPLEMENTING INHERITANCE IN JAVA


Inheritance basis The Super class Object reference variable can refer to Subclass Object.
class Animal { int legs=0; public void eat() { System.out.println(eating); }} class Cat extends Animal{ public void fish() { System.out.println(fish); } public static void main(String args[]) Animal obj = new Cat(); Obj.eat(); //Inheritance Obj.legs=4; // Inheritance Obj.fish(); // Compile error Super class reference cant //access Sub class method } }

CONSTRUCTORS
When you create a new instance (a new object) of a class using the new keyword, a constructor for that class is called. Constructors are used to initialize the instance variables (fields) of an object. Constructors are similar to methods, but with some important differences.

class ConstructorClass { public static void main(String args[]) ConstructorClass obj = new ConstructorClass(); ConstructorClass obj2= new ConstructorClass(); } }

CREATING CONSTRUCTORS
1) Parameter less Constructor.
class ConstructorClass { public static void main(String args[]) ConstructorClass obj = new ConstructorClass(); ConstructorClass obj2= new ConstructorClass(); } ConstructorClass(){ System.out.println(parameterless constructor); }

CREATING CONSTRUCTORS
2) Overloaded Constructors.
class ConstructorClass { public static void main(String args[]) ConstructorClass obj = new ConstructorClass(); ConstructorClass obj2= new ConstructorClass(25); ConstructorClass obj2= new ConstructorClass( A String); } ConstructorClass(){ System.out.println(parameterless constructor); } ConstructorClass(int x){ System.out.println(int parameter constructor); } ConstructorClass(String a){ System.out.println(String parameter constructor); }
}

FEATURES OF CONSTRUCTORS
Constructor name is class name. A constructors must have the same name as the class its in. If you don't define a constructor for a class, a default parameter less constructor is automatically created by the compiler. The default constructor calls the default parent constructor (super()) and initializes all instance variables to default value (zero for numeric types, null for object references, and false for booleans). Default constructor is created only if there are no constructors. If you define any constructor for your class, no default constructor is automatically created.

FEATURES OF CONSTRUCTORS
Class Super{} class Constructor extends Super{ public static void main(String args[]){ Constructor obj = new Constructor (); Constructor obj2= new Constructor (); /* it invoke the default constructor of super class and next default constructor of the Constructor Class.*/ }
}

DIFFERENCE BETWEEN METHODS AND CONSTRUCTORS

There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value. There is no return statement in the body of the constructor. The first line of a constructor must either be a call on another constructor in the same class (using this), or a call on the superclass constructor (using super). If the first line is neither of these, the compiler automatically inserts a call to the parameterless super class constructor.

DIFFERENCE BETWEEN METHODS AND CONSTRUCTORS


class Constructor{ public static void main(String args[]){ ConstructorClass obj = new ConstructorClass(); Obj.constructor(); } Constructor(){ System.out.println(int parameterless constructor); } int constructor(){ System.out.println(constructor method); return 10; } }

USING THIS()
this(...) - Calls another constructor in same class. Often a constructor with few parameters will call a constructor with more parameters, giving default values for the missing parameter.
class Constructor{ public static void main(String args[]){ ConstructorClass obj = new ConstructorClass(); }

Constructor(){ this(20); System.out.println(parameterless constructor); } Constructor(int x){ this(@); System.out.println(int parameter constructor); } Constructor(String s){ this(false); System.out.println(String parameter constructor); } Constructor(boolean b){ this(); System.out.println(boolean parameter constructor); }
}

USING SUPER()
super() Use super to call a constructor in a parent class. Calling the constructor for the superclass must be the first statement in the body of a constructor.

class SuperClass{ SuperClass (){ System.out.println(parameterless super constructor); } SuperClass(int x){ this(); System.out.println(int parameter super constructor); }} class Constructor{ public static void main(String args[]){ ConstructorClass obj = new ConstructorClass($); } Constructor(String s){ super(10); System.out.println(String parameter constructor); } Constructor(boolean b){ this(Apple); System.out.println(boolean parameter constructor); } }

POLYMORPHISM
The meaning of Polymorphism is something like one name many forms. Polymorphism enables one entity to be used as general category for different types of actions. The specific action is determined by, 1. Method Overloading 2. Method Over-riding 3. Interface methods implemented differently in different ways in sub-classes.

METHOD OVERRIDING
When a method in a subclass has the same return type and signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.

When an overridden method is called from within the subclass, always the method in the subclass is invoked.
The method inherited from the superclass will be hidden.

METHOD OVERRIDING
class Animal { public void eat() { System.out.println(eating); } } class Cat extends Animal{ public void eat() { System.out.println(cat eating fish ); } public static void main(String args[]) Animal obj = new Cat(); Obj.eat(); } }

COVARIANT RETURN TYPES


Covariant return type means overriden methods can put return type as subclass of super method Animal returnclass type. { public Animal eat()
{System.out.println(eating); return new Animal(); }} class Cat extends Animal{ public Cat eat() { System.out.println(cat eating fish ); return new Cat(); } public static void main(String args[]) Animal obj = new Cat(); Obj.eat(); } }

OVERLOADING METHODS
In Java it is possible to define two or more methods within the same class that share the same name, as long as the parameter declarations are different. The methods are said to be overloaded and the process is related to as method overloading.

OVERLOADING METHODS
class OverloadedMethods { public void test () { System.out.println(number of parameters); } int test (int a) { System.out.println(a = +a); Return a; } boolean test ()//Compile error parameter list must be changed { System.out.println(a = +a); Return false; } }

S-ar putea să vă placă și