350 likes | 479 Views
Java. Powered By: Arvind Department of Computer science and engineering Radha govind group of institutions,meerut. We have seen that by making a method final we ensure that the method is not redefined in a subclass. That is, the method can never be sub classed.
E N D
Java Powered By: Arvind Department of Computer science and engineering Radha govind group of institutions,meerut
We have seen that by making a method final we ensure that the method is not redefined in a subclass. That is, the method can never be sub classed. • Java allows us to do something that is exactly opposite to this. we can indicate that a method must always be redefined in a subclass, thus making overriding compulsory. Powerd By: Arvind
Abstract Class • If a class contain any abstract method then the class is declared as abstract class. An abstract class is never instantiated. It is used to provide abstraction. Although it does not provide 100% abstraction because it can also have concrete method. Syntax abstract class class_name{……………….} Powerd By: Arvind
Abstract Class • A class defined as Abstract can not be instantiated. Public abstract class Mobile{………………………} Mobile m=new Moblie(); You can not create object of this class. Powerd By: Arvind
Abstract at Method Level • When a method is marked as abstract, there should not be any implementation for the method. Syntax: Public abstract void call(); // this is valid. Public abstract void call() // this is not valid{} Powerd By: Arvind
Abstract classes need not have abstract methods. • If there is an abstract method, the whole should be declared as abstract. public abstract class Mobile{ public void call();} Public class Mobile{ public abstract void call();} In this class there is no definition or implementation of void call() hence it is not valid to use abstract method in concrete class and it is invalid. Powerd By: Arvind
Abstract method • Method that are declared without any body within an abstract class is known as abstract method. • The method body will be defined by its subclass. • Abstract method can never be final and static. • Any class that extends an abstract class must implement all the abstract methods declared by the super class. abstract return_type function_name(); Powerd By: Arvind
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(); } • } • Output : this is callme Powerd By: Arvind
Abstract class with normal(concrete) method • Normal methods are also known as concrete methods. • Abstract classes can also have normal methods with definitions, along with abstract methods. Powerd By: Arvind
abstract class A{ abstract void callme(); public void normal() { System.out.println("this is concrete method"); }}class B extends A{ void callme() { System.out.println("this is callme."); } public static void main(String[] args) { B b=new B();b.callme();b.normal(); }}Output: this is call me this is concrete method Powerd By: Arvind
Abstract at class level • A concrete class extending an abstract class should override all the abstract methods. • If a subclass does not override an abstract method, it should mark itself as a abstract to delegates the responsibility to its subclasses. Public abstract class Mobile{ public abstract void call();} Powerd By: Arvind
Public class Smartphone extends Mobile{public void call(){ }} Powerd By: Arvind
Points to remember: • Abstract classes are not Interfaces. They are different, we will study this when we will study Interfaces. • An abstract class must have an abstract method. • Abstract classes can have Constructors, Member variables and Normal methods. • Abstract classes are never instantiated. Powerd By: Arvind
When you extend Abstract class with abstract method, you must define the abstract method in the child class, or make the child class abstract. Powerd By: Arvind
Abstraction using abstract class • Abstraction is an important feature of OOPS. It means hiding complexity. Abstract class is used to provide abstraction. Although it does not provide 100% abstraction because it can also have concrete method. Lets see how abstract class is used to provide abstraction. Powerd By: Arvind
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) { Vehicle v = new Car(); v.engine(); }} Powerd By: Arvind
Output: car engine Here by casting instance of Car type to Vehicle reference, we are hiding the complexity of Car type under Vehicle. Now the Vehicle reference can be used to provide the implementation but it will hide the actual implementation process. Powerd By: Arvind
Wrapper classes • Vectors can not handle primitive data types like int, float, long, char, double. • Primitive data type may be converted into object by using wrapper classes Powerd By: Arvind
package myclasses;import java.util.*;public class VectorWrap{ public static void main(String[] args) {Vector v=new Vector(3); int n=100;v.add(n);v.add(1);v.add(3);v.add(10); System.out.println("The values in vector:");for(int i=0;i<v.size();i++){ System.out.println(v.get(i));} }} Powerd By: Arvind
As the name says, a wrapper class wraps (encloses) around a data type and gives it an object appearance. • Wherever, the data type is required as an object, this object can be used. Wrapper classes include methods to unwrap the object and give back the data type. • It can be compared with a chocolate. The manufacturer wraps the chocolate with some foil or paper to prevent from pollution. The user takes the chocolate, removes and throws the wrapper and eats it. Powerd By: Arvind
Observe the following conversion. int k = 100;Integer it1 = new Integer(k). The int data type k is converted into an object, it1 using Integer class. The it1 object can be used in Java programming wherever k is required an object. • The following code can be used to unwrap (getting back int from Integer object) the object it1. int m = it1.intValue();System.out.println(m*m); // prints 10000intValue() is a method of Integer class that returns an int data type. Powerd By: Arvind
List of Wrapper classes • In the above code, Integer class is known as a wrapper class (because it wraps around int data type to give it an impression of object). To wrap (or to convert) each primitive data type, there comes a wrapper class. Eight wrapper classes exist in java.lang package that represent 8 data types. Following list gives. - See more at: http://way2java.com/java-lang/wrapper-classes/#sthash.a8ayCAVp.dpuf Powerd By: Arvind
public class WrappingUnwrapping{public static void main(String args[]) {// data typesbyte grade = 2;int marks = 50; float price = 8.6;float double rate = 50.5; // data type wrapping Byte g1 = new Byte(grade); Integer m1 = new Integer(marks); Float f1 = new Float(price); Double r1 = new Double(rate); // let us print the values from objects Powerd By: Arvind
System.out.println("Values of Wrapper objects (printing as objects)"); System.out.println("Byte object g1: " + g1); System.out.println("Integer object m1: " + m1); System.out.println("Float object f1: " + f1);System.out.println("Double object r1: " + r1); // objects to data types (retrieving data types from objects) //unwrappingbyte bv = g1.byteValue(); iv = m1.intValue(); float fv = f1.floatValue(); double dv = r1.doubleValue(); // let us print the values from data types Powerd By: Arvind
System.out.println("Unwrapped values (printing as data types)"); System.out.println("byte value, bv: " + bv); System.out.println("int value, iv: " + iv); System.out.println("float value, fv: " + fv);System.out.println("double value, dv: " + dv);}} Powerd By: Arvind
Auto boxing and Unboxing • Auto boxing and Unboxing features was added in Java5. • Auto boxing is a process by which primitive type is automatically encapsulated(boxed) into its equivalent type wrapper. • Auto-Unboxing is a process by which the value of object is automatically extracted from a type wrapper. Powerd By: Arvind
class Test{ public static void main(String[] args) { Integer iob = 100; //auto boxing of int int i=iob; //unboxing of Integer System.out.println(i+“ ”+iob); Character cob=‘a’; //auto boxing of char char ch=cob; // Auto unboxing of Character }} Output:100 100a a Powerd By: Arvind
Autoboxing / Unboxing in expressions • Whenever we use object of Wrapper class in an expression, automatic unboxing and boxing is done by JVM. Integer iOb;iOb = 100; //auto boxing of int ++iob; When we perform increment operation on Integer object, it is first unboxed, then incremented and then again reboxed into Integer type object. Powerd By: Arvind
Benefits of Autoboxing / Unboxing • Autoboxing / Unboxing lets us use primitive types and Wrapper class objects interchangeably. • We don't have to perform Explicit typecasting. • It helps prevent errors, but may lead to unexpected results sometimes. Hence must be used with care. Powerd By: Arvind
The End Powerd By: Arvind