Oops (inheritance&interface)

25
Main Concepts of OOP Encapsulation Inheritance Polymorphism Abstraction

description

 

Transcript of Oops (inheritance&interface)

Page 1: Oops (inheritance&interface)

Main Concepts of OOP

• Encapsulation

• Inheritance

• Polymorphism

• Abstraction

Page 2: Oops (inheritance&interface)

Encapsulation

• Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods.

• The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code.

• Encapsulation gives maintainability, flexibility and extensibility to our code.

Page 3: Oops (inheritance&interface)

Access Specifiers in java

• public

• private

• protected

• Default

Page 4: Oops (inheritance&interface)

Public access specifier

• A class, method, constructor, interface etc., declared public can be accessed from any other class.

• Therefore fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.

• If the public class we are trying to access is in a different package, then the public class still need to be imported.

Page 5: Oops (inheritance&interface)

Private access specifiers

• Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.

• Private access modifier is the most restrictive access level. Class and interfaces cannot be private.

• Variables that are declared private can be accessed outside the class if public getter methods are present in the class.

• Using the private modifier is the main way that an object encapsulates itself and hide data from the outside world.

Page 6: Oops (inheritance&interface)

Sample code for private access specifier

Page 7: Oops (inheritance&interface)

Protected access specifier

• Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.

• The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.

Page 8: Oops (inheritance&interface)

Sample code for protected access specifier

Page 9: Oops (inheritance&interface)

Default specifier

• The default specifier is used to make a class visible to all the other classes in its package but not visible to classes from other packages

Page 10: Oops (inheritance&interface)

Inheritance

• Inheritance is a compile-time mechanism in Java that allows you to extend a class with another class

• The keyword extends is used for inheriting a Class.

• The class which extends some other class is called as derived class

• The base class is called as the super class or parent class.

Page 11: Oops (inheritance&interface)

Types of inheritance

• Simple inheritance

• Multilevel inheritance

• Hierarchical inheritance

• Hybrid inheritance

Page 12: Oops (inheritance&interface)

Simple inheritance

• There are only 1 base class and 1 derived class

• Syntax:

Class Parentclass

{

}

Class Dclass extendsParentclass

{

}

Page 13: Oops (inheritance&interface)

Simple inheritance sample code and output

Page 14: Oops (inheritance&interface)

Multilevel inheritance

• It contains derived classes which are in turn base class to another class.

class Base

{

}

class Derived1 extends Base

{

}

class Derived2 extends Derived1

{

}

Page 15: Oops (inheritance&interface)

Multilevel inheritance sample code and output

Page 16: Oops (inheritance&interface)

Hierarchical inheritance

• It contains one base class and more than one derived class.

class Base

{

}

class Derived1 extends Base

{

}

class Derived2 extends Base{ }

Page 17: Oops (inheritance&interface)

Sample code and output for Hierarchical inheritance

Page 18: Oops (inheritance&interface)

Hybrid inheritance

• It is a combination of any two or more inheritances.

Page 19: Oops (inheritance&interface)

Sample code for hybrid inheritance

Page 20: Oops (inheritance&interface)

Interface

• An interface in Java is similar to a class, but the body of an interface can include only abstract methods and final fields (constants).

• A class implements an interface by providing code for each method declared by the interface.

• The keyword used in interface concepts are interface and implements

Page 21: Oops (inheritance&interface)

Syntax for interface

interface interfacename

{

method_declaration();

}

class A implements interfacename

{

method_declaration(){ }

}

Page 22: Oops (inheritance&interface)

Interface sample code

Page 23: Oops (inheritance&interface)

Interface sample output

Page 24: Oops (inheritance&interface)

Any queries??

Page 25: Oops (inheritance&interface)

Thank you